我想在VB.net中更新我的数据库数据,但出现错误语法

时间:2019-03-05 16:31:38

标签: database vb.net ms-access

这是我的问题:

单击更新按钮时,我不知道如何解决此错误:

我的错误消息是:

  

“错误:联合查询中的语法错误”

这是我的代码:

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

错了吗?

This is my Error message

This is My code

1 个答案:

答案 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