使用Microsoft JET OLEDB 4.0添加用户记录vb.net 2010数据库(MSaccess)

时间:2014-08-25 16:29:28

标签: database vb.net oledb oledbconnection

你好我在我的数据库中保存记录时遇到问题,当我调试程序时,它表示" OLEDB Execption未处理,更新语句中的语法错误" 你能帮助我吗?

这是正确的代码吗?

Imports System.Data.OleDb
Public Class Form2
    Dim contr As String = "Provider= Microsoft.Jet.OLEDB.4.0; Data Source= C:\Users\Administrator\Desktop\Users.mdb;"

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New OleDbConnection(contr)
        con.Open()
        Dim cmd As New OleDbCommand("UPDATE Usertable SET Username = @Username, Password = @Password WHERE id = @id", con)
        cmd.Parameters.AddWithValue("@Username", TextBox1.Text)
        cmd.Parameters.AddWithValue("@Password", TextBox2.Text)
        cmd.Parameters.AddWithValue("@Id", Label3.Text)
        cmd.ExecuteNonQuery()

        con.Close()

    End Sub
    End Class

2 个答案:

答案 0 :(得分:0)

密码是MS-Access的reserved keyword。用方括号括起来

答案 1 :(得分:0)

Access / OLE不喜欢命名参数。您需要使用?占位符:

UPDATE Usertable SET Username = ?, Password = ? WHERE id = ?

然后,要将每个参数值与占位符匹配,必须按照查询中使用占位符的顺序将它们添加到Parameters集合,而不是按名称添加。