针对Access数据库的Update语句中的语法错误(使用OleDbCommandBuilder)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了针对Access数据库的Update语句中的语法错误(使用OleDbCommandBuilder)相关的知识,希望对你有一定的参考价值。
我多年来一直在研究这个问题!我有一个MSAccess 2007数据库,我使用Visual Studio 2010和Visual Basic编写一个从数据库读取和写入的应用程序。读取工作正常,但当我尝试将更新的密码写回数据库时,它会失败。最初我只有一个简单的'语法错误',这没什么帮助,但通过一些研究,我注意到访问数据库似乎有一个名为密码的列的问题。我重命名并再次尝试,现在我得到这个错误了
da.Update(DS, “All_Users的”)
执行命令。
来自VS的完整错误消息是:Syntax error (missing operator) in query expression '((ID = ?) AND ((? = 1 AND Forename IS NULL) OR (Forename = ?)) AND ((? = 1 AND Surname IS NULL) OR (Surname = ?)) AND ((? = 1 AND User_Level IS NULL) OR (User_Level = ?)) AND ((? = 1 AND Last Logon IS NULL) OR (Last Logon = ?)) AND ((? = 1 AND Allow IS NU'.
我的代码如下:
Private Sub btnSave_Click(snder as System.Object, e As System.EventArgs) Handles btnSave.Click
Dim Con As New OleDb.OleDbConnection
Dim ConString As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim Sql As String = "SELECT * FROM tblUsers"
'
ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
Application.StartupPath & "DataUsers.accdb;Jet OLEDB:Database Password=---------;"
Con.ConnectionString = ConString
Con.Open()
da = New OleDb.OleDbDataAdapter(Sql, Con)
da.Fill(ds, "All_Users")
'Now loop through the records until you find the one for this user
For i = 0 To ds.Tables("All_Users").Rows.Count - 1
If ds.Tables("All_Users").Rows(i).Item(0).ToString = CurrentUser.ID Then
ds.Tables("All_Users").Rows(i).Item(6) = txtConfirmPassword.Text
End If
Next
CurrentUser.Password = txtConfirmPassword.Text
'
Dim cb As New OleDb.OleDbCommandBuilder(da)
da.Update(ds, "All_Users")
'
Con.Close()
MessageBox.Show("Your password has been sucessfully updated.", "Success", _
MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Me.Close()
当使用OleDbCommandBuilder
时总是设置.QuotePrefix
和.QuoteSuffix
属性:
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
以上是关于针对Access数据库的Update语句中的语法错误(使用OleDbCommandBuilder)的主要内容,如果未能解决你的问题,请参考以下文章