我添加了修改我网站上的密码和电子邮件地址的选项。我有一个微软的Access数据库。它有列" EmailAddress "," 密码"和" 用户名"在" 登录"表
'For testing purposes, lets say :
VB_PasswordTXT = "1000:8NHLJo5oIDugFdzscuMrNSKyoNTRa0kI:6NSlpqGYC4zU2BG6cfJaHaPgVRCDPCc2"
VB_UserNameTXT = "Test123"
VB_EmailTXT = "a@mail.com"
Using thisConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""path""")
Using thisCommand As OleDbCommand = thisConnection.CreateCommand
' Open connection object
thisConnection.Open()
' Initialize SQL SELECT command to retrieve desired data
thisCommand.CommandText = "UPDATE Login SET EmailAddress = @EmailAddress, Password = @Password " & _
"WHERE Username = @Username"
thisCommand.Parameters.AddWithValue("@Username", VB_UserNameTXT)
thisCommand.Parameters.AddWithValue("@EmailAddress", VB_EmailTXT)
thisCommand.Parameters.AddWithValue("@Password", VB_PasswordTXT)
' Create a DataReader object based on previously defined command object
Dim thisReader As OleDbDataReader = thisCommand.ExecuteReader() 'Crashes here
If thisReader.Read() Then
End If
thisReader.Close()
thisConnection.Close()
End Using
End Using
UPDATE命令中的语法错误。 (法语免费翻译)
(据我所知),问题来自UPDATE
语法。有什么问题?
答案 0 :(得分:1)
密码是MS-Access中的reserved keyword 当你有一个以这种方式命名的字段时,你应该将它括在方形的方块中
thisCommand.CommandText = "UPDATE Login SET EmailAddress = @EmailAddress, " & _
"[Password] = @Password " & _
"WHERE Username = @Username"
我建议,如果仍有可能,请更改该名称
而且,当ExecuteReader工作时,用于UPDATE / INSERT / DELETE sql的正确方法是
thisCommand.ExecuteNonQuery()
调用ExecuteReader并不好,因为你强制它创建一个更新时不需要的OleDbDataReader。
修改强>
阅读您的上一条评论我注意到您在集合中以错误的顺序添加了参数。 OleDb不会通过名称识别参数,而是通过它们在参数集合中的位置来识别。您应该按照各自的占位符在命令文本中显示的确切顺序添加它们(将用户名移动到最后添加的用户名)
thisCommand.CommandText = "UPDATE Login SET EmailAddress = @EmailAddress, " & _
"[Password] = @Password " & _
"WHERE Username = @Username"
thisCommand.Parameters.AddWithValue("@EmailAddress", VB_EmailTXT)
thisCommand.Parameters.AddWithValue("@Password", VB_PasswordTXT)
thisCommand.Parameters.AddWithValue("@Username", VB_UserNameTXT)