对Access数据库的更新语句中的语法错误(使用OleDbCommandBuilder) [英] Syntax error in Update statement against Access database (using OleDbCommandBuilder)

查看:198
本文介绍了对Access数据库的更新语句中的语法错误(使用OleDbCommandBuilder)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理这个问题,我有一个MSAccess 2007数据库,我使用Visual Studio 2010和Visual Basic编写从数据库读取和写入的应用程序。读取工作很好,但是当我尝试将更新的密码写回数据库时,它会失败。最初我只是一个简单的语法错误,这不是有帮助的,但是有一点研究我注意到访问数据库似乎有一个名为密码的列的问题。我重命名并再次尝试,现在我收到这个错误,当

I have been working on this issue for ages! I have an MSAccess 2007 db and I am using Visual Studio 2010 and Visual Basic to write an application that reads and writes from the database. The read works just fine but when I try and write an updated password back into the database, it fails. Initially I had just a plain 'Syntax Error' which wasn't that helpful, but with a bit of research I noted that access databases seem to have an issue with columns called password. I renamed that and tried again, now I get this error when the


da.Update(ds,All_Users)

da.Update(ds,"All_Users")

命令被执行。

来自VS的完整错误消息是:
查询表达式'((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'。

The full error message from VS is : 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()


推荐答案

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

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

这篇关于对Access数据库的更新语句中的语法错误(使用OleDbCommandBuilder)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆