我多年来一直在研究这个问题!我有一个 MSAccess 2007 数据库,我正在使用 Visual Studio 2010 和 Visual Basic 编写一个从数据库读取和写入的应用程序。读取工作正常,但是当我尝试将更新后的密码写回数据库时,它失败了。最初我只有一个简单的“语法错误”,这并没有多大帮助,但经过一些研究,我注意到 Access 数据库似乎对称为密码的列存在问题。我重命名并重试,现在当
da.Update(ds,"All_Users")
命令被执行。
来自 VS 的完整错误消息是:
查询表达式 '((ID = ?) AND ((? = 1 AND Forename IS NULL) OR (Forename = ?)) AND ((? = 1 AND Surname IS NULL) OR ( 中存在语法错误(缺少运算符)姓氏 = ?)) AND ((? = 1 AND 用户级别为 NULL) OR (User_Level = ?)) AND ((? = 1 AND 上次登录为 NULL) OR (上次登录 = ?)) AND ((? = 1 AND允许 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 & "\Data\Users.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 = "]"
关于vb.net - 针对 Access 数据库的 Update 语句中的语法错误(使用 OleDbCommandBuilder),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20053084/