在MS Access 2003中INSERT到用户表中的语法错误

时间:2012-03-24 14:56:04

标签: asp.net sql vb.net ms-access-2003

将VB.NET与ASP.NET和ms-access 2003数据一起使用,我正在尝试将数据从Web表单输入到db.mdb中名为“USER”的表中。

我试过这段代码:

 Protected Sub btnCreateAccount_Click(sender As Object, e As System.EventArgs) Handles btnCreateAccount.Click

    Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\WebSite3\db.mdb;User Id=admin;Password=;")

    Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO USER (Name, Surname, Username, Country, TelNo, Password, Address) VALUES (?, ?, ?, ?, ?, ?, ?)", conn)

    If txtPass.Text = txtCPass.Text Then

        cmd.Parameters.Add("@Name", OleDbType.VarChar, 255).Value = txtName.Text
        cmd.Parameters.Add("@Surame", OleDbType.VarChar, 255).Value = txtSurname.Text
        cmd.Parameters.Add("@Address", OleDbType.VarChar, 255).Value = txtAddress.Text
        cmd.Parameters.Add("@Country", OleDbType.VarChar, 255).Value = txtCountry.Text
        cmd.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = txtUsername.Text
        cmd.Parameters.Add("@Password", OleDbType.VarChar, 255).Value = txtPass.Text
        cmd.Parameters.Add("@TelNo", OleDbType.Integer).Value = txtTelNo.Text

        Try

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

        Catch ex As OdbcException
            Throw ex
        Finally
            conn.Close()
            lblAccount.Visible = True


        End Try

    End If

End Sub

但是它给我一个错误信息:

enter image description here

有关问题可能原因的任何建议吗?

1 个答案:

答案 0 :(得分:3)

UserPassword是保留关键字。更改名称,或在查询中的名称周围使用方括号:

    Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO [USER] (Name, Surname, Username, Country, TelNo, [Password], Address) VALUES (?, ?, ?, ?, ?, ?, ?)", conn)

然后,对于您将遇到的下一个错误:由于参数未在查询中命名,因此Parameters集合中的参数对象必须按查询中使用的顺序添加