【发布时间】:2013-12-01 21:58:56
【问题描述】:
我多年来一直在研究这个问题!我有一个 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()
【问题讨论】:
-
您似乎有一个名为 Last Logon 的字段。将该字段重命名为 Last_Logon(以消除空格)并再次尝试您的代码。
标签: vb.net visual-studio-2010 ms-access dataset