针对Access数据库的Update语句中的语法错误(使用OleDbCommandBuilder)

时间:2013-11-18 16:44:33

标签: vb.net visual-studio-2010 ms-access dataset

我多年来一直在研究这个问题!我有一个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 & "\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()

1 个答案:

答案 0 :(得分:2)

使用OleDbCommandBuilder时始终设置.QuotePrefix.QuoteSuffix属性:

Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"