OleDbCommandBuilder创建导致“语法错误”的SQL语句

时间:2013-10-13 06:51:35

标签: vb.net ms-access oledb

有人可以解释一下为什么,当我点击“提交”按钮时,我收到错误

  

INSERT INTO语句中的语法错误。

这是代码。

Public Class Form3
    Dim inc As Integer
    Dim MaxRows As Integer
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter

    Dim sql As String

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\TA_Officers.mdb"
        con.Open()
        sql = "SELECT * from TA_OFFICER"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "TA_Officers")
        con.Close()

        MaxRows = ds.Tables("TA_Officers").Rows.Count
        inc = -1
    End Sub

    Private Sub NavigateRecords()
        txtFName.Text = ds.Tables("TA_Officers").Rows(inc).Item(1)
        txtMInitial.Text = ds.Tables("TA_Officers").Rows(inc).Item(2)
        txtLName.Text = ds.Tables("TA_Officers").Rows(inc).Item(3)
        txtContact.Text = ds.Tables("TA_Officers").Rows(inc).Item(4)
        txtEmail.Text = ds.Tables("TA_Officers").Rows(inc).Item(5)
        txtPosition.Text = ds.Tables("TA_Officers").Rows(inc).Item(6)
        txtCourse.Text = ds.Tables("TA_Officers").Rows(inc).Item(7)
        txtAddress.Text = ds.Tables("TA_Officers").Rows(inc).Item(8)
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        BtnCommit.Enabled = True
        BtnAdd.Enabled = False
        BtnUpdate.Enabled = False
        BtnDel.Enabled = False

        txtPosition.Clear()
        txtLName.Clear()
        txtFName.Clear()
        txtMInitial.Clear()
        txtAddress.Clear()
        txtCourse.Clear()
        txtEmail.Clear()
        txtContact.Clear()
    End Sub

    Private Sub RdMember_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdMember.CheckedChanged
        Label2.Visible = False
        txtPosition.Visible = False
    End Sub

    Private Sub RdOfficer_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdOfficer.CheckedChanged
        Label2.Visible = True
        txtPosition.Visible = True
    End Sub

    Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)

        ds.Tables("TA_Officers").Rows(inc).Item(1) = txtFName.Text
        ds.Tables("TA_Officers").Rows(inc).Item(2) = txtMInitial.Text
        ds.Tables("TA_Officers").Rows(inc).Item(3) = txtLName.Text
        ds.Tables("TA_Officers").Rows(inc).Item(4) = txtContact.Text
        ds.Tables("TA_Officers").Rows(inc).Item(5) = txtEmail.Text
        ds.Tables("TA_Officers").Rows(inc).Item(6) = txtPosition.Text
        ds.Tables("TA_Officers").Rows(inc).Item(7) = txtCourse.Text
        ds.Tables("TA_Officers").Rows(inc).Item(8) = txtAddress.Text

        da.Update(ds, "TA_Officers")

        MsgBox("Data Updated!")
    End Sub

    Private Sub BtnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDel.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)

        ds.Tables("TA_Officers").Rows(inc).Delete()
        MaxRows = MaxRows - 1

        inc = 0
        NavigateRecords()
        da.Update(ds, "TA_Officers")
    End Sub

    Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
        BtnCommit.Enabled = False
        BtnAdd.Enabled = True
        BtnUpdate.Enabled = True
        BtnDel.Enabled = True

        inc = 0
        NavigateRecords()
    End Sub

    Private Sub BtnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCommit.Click
        If inc <> -1 Then

            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim dsNewRow As DataRow

            dsNewRow = ds.Tables("TA_Officers").NewRow()

            dsNewRow.Item("Firstname") = txtFName.Text
            dsNewRow.Item("Middleinitial") = txtMInitial.Text
            dsNewRow.Item("Lastname") = txtLName.Text
            dsNewRow.Item("Mobilenumber") = txtContact.Text
            dsNewRow.Item("Emailaddress") = txtEmail.Text
            dsNewRow.Item("Position") = TxtPosition.Text
            dsNewRow.Item("Course") = txtCourse.Text
            dsNewRow.Item("Address") = txtAddress.Text

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

            MsgBox("New Record added to the Database")
            BtnCommit.Enabled = False
            BtnAdd.Enabled = True
            BtnUpdate.Enabled = True
            BtnDel.Enabled = True
        End If
    End Sub

    Private Sub BtnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBack.Click
        Form2.Show()
        Me.Close()
    End Sub

    Private Sub TA_MEMBERSBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.TA_MEMBERSBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.TA_OfficersDataSet)

    End Sub
End Class

3 个答案:

答案 0 :(得分:2)

Position是JET 4.0的保留字 - 尝试将列重命名为其他内容,例如prog_position。 Access通常允许您使用保留名称创建列,但是当您在代码中访问它时,JET会阻止它并在不具体的情况下为您提供通用的Syntax error in INSERT INTO Statement错误。有关保留字的更多信息,请查看http://support.microsoft.com/kb/248738。这是一种商品惯例,总是在列名称前加上简短的名称,这样就不会遇到冲突或保留字问题。

答案 1 :(得分:2)

正如Makita所提到的,你的问题源于Position是Jet / ACE SQL中的保留字。解决方案是在创建OleDbCommandBuilder对象后添加以下两行代码:

cb.QuotePrefix = "["
cb.QuoteSuffix = "]"

这会告诉OleDbCommandBuilder生成像这样的SQL语句

INSERT INTO [TA_OFFICER] ([FirstName], ...

...而不是

INSERT INTO TA_OFFICER (FirstName, ...

Position列名称包含在这些方括号中将告诉Jet数据库引擎它是列名,而不是关键字。

答案 2 :(得分:0)

当您在代码和数据库中使用不同的数据类型时,可能会发生此类错误。 检查日志可以解释一下。