语法错误INSERT INTO语句

时间:2015-07-01 12:40:05

标签: vb.net

我使用VB.net和MS Access 2007作为后端。 我试图保存时遇到错误。 这是我的保存按钮代码:

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            con = New OleDbConnection(cn)
            con.Open()
            Dim cb As String = "INSERT INTO LoginDetails (EmpID, EmpName, Username, Password) VALUES (@d1, @d2, @d3, @d4)"
            'Dim cb As String = " INSERT INTO LoginDetails (EmpID, EmpName, Username, Password) VALUES ('" & cboEmpID.Text & "', '" & txtEmpName.Text & "', '" & txtUsername.Text & "', '" & txtPassword.Text & "')"
            cmd = New OleDbCommand(cb)
            cmd.Connection = con
            cmd.Parameters.Add(New OleDbParameter("@d1", OleDbType.VarChar, 30, "EmpID"))
            cmd.Parameters.Add(New OleDbParameter("@d2", OleDbType.VarChar, 30, "EmpName"))
            cmd.Parameters.Add(New OleDbParameter("@d3", OleDbType.VarChar, 30, "Username"))
            cmd.Parameters.Add(New OleDbParameter("@d4", OleDbType.VarChar, 30, "Password"))
            cmd.Parameters("@d1").Value = cboEmpID.Text
            cmd.Parameters("@d2").Value = txtEmpName.Text
            cmd.Parameters("@d3").Value = txtUsername.Text
            cmd.Parameters("@d4").Value = txtPassword.Text
            cmd.ExecuteNonQuery()
            MessageBox.Show("Registration completed", "Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information)
            cmdSave.Enabled = False
            cboEmpID.Focus()
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

1 个答案:

答案 0 :(得分:0)

尝试更简单:

cmd.Parameters.Add(" @ d1",OleDbType.VarChar,30).Value = cboEmpID.Text