添加到访问数据库时INSERT into语句中的语法错误

时间:2014-03-12 10:32:20

标签: vb.net sql-insert

有人请帮我找到代码上的错误。 错误是在我的尝试中的行,并捕获我试图在我的数据库Access上添加记录。错误是“Syntax error in INSERT into statement”。我已经尝试过使用

ds.Tables("Users").Rows.Add(dsNewRow)
            da.Update(ds, "Users")

我的选民登记工作正常。 idk为什么它不能在这个表格上工作(用户注册)。

Imports System.Data.OleDb

Public Class UserRegister
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String


Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click

End Sub

Private Sub UserRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    dbProvider = " PROVIDER=Microsoft.jet.OLEDB.4.0;"
    dbSource = "Data Source= C:\Users\Ronel\Documents\database\CSdatabase.mdb"

    con.ConnectionString = dbProvider & dbSource
    con.Open()
    sql = "SELECT*FROM tblUsers"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "Users")
    MsgBox("Database now Open")
    con.Close()
    'MsgBox("Database now Close")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim cb As New OleDb.OleDbCommandBuilder(da)
    Dim dsNewRow As DataRow

    Dim empty =
    Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0)
    If empty.Any Then
        MessageBox.Show(String.Format("PLEASE FILL ALL FIELDS:"))
    Else
        dsNewRow = ds.Tables("Users").NewRow()
        dsNewRow.Item("Username") = TextBoxUser.Text
        dsNewRow.Item("Password") = TextBoxPass.Text
        dsNewRow.Item("LastName") = TextBoxFN.Text
        dsNewRow.Item("GivenName") = TextBoxGN.Text
        dsNewRow.Item("MiddleName") = TextBoxMN.Text

        ' Try
        ds.Tables("Users").Rows.Add(dsNewRow)
        da.Update(ds, "Users")
        '   Catch ex As Exception
        MsgBox("Error updating")
        '  End Try

        ' Me.Dispose()
        'Comelec.Show()
    End If

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我看到一些问题。首先,您的SQL语句需要一些空格。而不是sql = "SELECT*FROM tblUsers",请使用sql = "SELECT * FROM tblUsers"

其次,使用OleDbCommandBuilder时,代码使用您为SELECT提供的DataAdapter语句连接到数据库,并使用它为Update,Delete,生成必要的SQL,和插入语句。需要打开与数据库的连接才能实现此操作 - 在运行CommandBuilder时关闭连接。这可能是语法错误的来源。

CommandBuilder将使用SELECT中指定的所有字段创建这些语句。如果您的Users表中的字段不是您尝试填充的五个字段,那么OleDbCommandBuilder仍将构建一个INSERT语句,该语句也将尝试填充这些字段。根据表格的结构,如果不填充必填字段,可能会导致行被拒绝。要查看正在生成的SQL语句,可以在使用CommandBuilder后查看DataAdapter对象的属性:

con.Open
sql = "SELECT * FROM tblUsers"
da = New OleDb.OleDbDataAdapter(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da)
Debug.Print("SELECT: " & da.SelectCommand.CommandText)
Debug.Print("UPDATE: " & da.UpdateCommand.CommandText)
Debug.Print("DELETE: " & da.DeleteCommand.CommandText)
Debug.Print("INSERT: " & da.InsertCommand.CommandText)

这里有一些关于CommandBuilders的额外阅读:http://msdn.microsoft.com/en-us/library/tf579hcz%28v=vs.110%29.aspx

尝试一下:

Imports System.Data.OleDb

Public Class UserRegister
    Const dbProvider As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    Dim dbSource As String = "Data Source=C:\Users\Ronel\Documents\database\CSdatabase.mdb"
    Dim con As New OleDb.OleDbConnection(dbProvider & dbSource)
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    'Specify only the fields you need
    Dim sql As String = "SELECT UserName, Password, LastName, GivenName, MiddleName FROM tblUsers"


    Private Sub UserRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.Open()
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Users")
        ' Get commands now, while connection is open. Do this once when the form is loaded, not every time button is clicked.
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        con.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Validation code goes here
        If empty.Any Then
            MessageBox.Show(String.Format("PLEASE FILL ALL FIELDS:"))
        Else
            Dim dsNewRow As DataRow
            dsNewRow = ds.Tables("Users").NewRow()
            dsNewRow.Item("Username") = TextBoxUser.Text
            dsNewRow.Item("Password") = TextBoxPass.Text
            dsNewRow.Item("LastName") = TextBoxFN.Text
            dsNewRow.Item("GivenName") = TextBoxGN.Text
            dsNewRow.Item("MiddleName") = TextBoxMN.Text

            Try
                con.Open()
                ds.Tables("Users").Rows.Add(dsNewRow)
                da.Update(ds, "Users")
            Catch ex As Exception
                MsgBox("Error updating: " & Err.Description)
            Finally
                con.Close()
            End Try
        End If
    End Sub
End Class