这是我的问题:
单击更新按钮时,我不知道如何解决此错误:
我的错误消息是:
“错误:联合查询中的语法错误”
这是我的代码:
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
TestConnection()
Try
Dim cmd As OleDbCommand
Dim sql As String
sql = "(UPDATE tblUsers SET Username = '" & txtUserName.Text & "', Password = '" & txtUserPassword.Text &
"', Usertype = '" & cbousertype.Text & "', WHERE UserID = '" & txtUserID.Text & "');"
cmd = New OleDbCommand(sql, Conn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Error: " & ex.Message)
End Try
End Sub
错了吗?
答案 0 :(得分:0)
现在我的问题已经解决了,非常感谢 我更改了代码以使用参数,然后工作了 现在我的代码是:
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
TestConnection()
Dim cmd As OleDbCommand
Dim sql As String
sql = "UPDATE tblUsers SET Username=?, [Password]=?, Usertype=? where UserID=?"
cmd = New OleDbCommand(sql, Conn)
cmd.Parameters.AddWithValue("@p1", txtUserName.Text)
cmd.Parameters.AddWithValue("@p2", txtUserPassword.Text)
cmd.Parameters.AddWithValue("@p3", cbousertype.Text)
cmd.Parameters.AddWithValue("@p4", txtUserID.Text)
cmd.ExecuteNonQuery()
MsgBox("Data Has Been Updated", MsgBoxStyle.Information, "Updated")
ShowUser()
End Sub