INSERT INTO语句中的语法错误。 VB

时间:2018-04-14 11:54:56

标签: vb.net visual-studio-2017

我在代码中没有看到我做错了什么。我尝试了在谷歌中找到的所有可能的解决方案,但仍然出现相同的错误“INSERT INTO语句中的语法错误。”

 Private Sub Add_row_Click(sender As Object, e As EventArgs) Handles Add_row.Click

        Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\carol\Documents\VBexcel\customer masterlist.xlsx;Extended Properties = ""Excel 12.0 Xml;HDR=YES"""
        Dim selectStatement As String = "SELECT * FROM [Sheet1$]"
        Dim adapter As New OleDbDataAdapter(selectStatement, connectionString)
        Dim ds As New DataSet("[Sheet1$]")
        adapter.FillSchema(ds, SchemaType.Source, "[Sheet1$]")
        adapter.Fill(ds, "[Sheet1$]")

        Dim excelData As New DataTable
        DataGridView_excel.DataSource = excelData

        excelData = ds.Tables("[Sheet1$]")
        Dim drCurrent As DataRow
        drCurrent = excelData.NewRow
        drCurrent("First Name") = TextBox1.Text
        drCurrent("Last Name") = TextBox2.Text
        drCurrent("Address") = TextBox3.Text
        drCurrent("Number") = TextBox4.Text
        ' more fields go here

        excelData.Rows.Add(drCurrent)
        Dim objCommandBuilder As New OleDb.OleDbCommandBuilder(adapter)
        adapter.Update(ds, "[Sheet1$]")


 End Sub 

此代码是我收到错误的地方:

adapter.Update(ds, "[Sheet1$]")

1 个答案:

答案 0 :(得分:0)

我要感谢@jmcilhinney的提示。它有效!!

Private Sub Add_row_Click(sender as Object,e As EventArgs)处理Add_row.Click

    Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\carol\Documents\VBexcel\customer masterlist.xlsx;Extended Properties = ""Excel 12.0 Xml;HDR=YES"""
    Dim selectStatement As String = "SELECT * FROM [Sheet1$]"
    Dim adapter As New OleDbDataAdapter(selectStatement, connectionString)
    Dim ds As New DataSet("[Sheet1$]")
    adapter.FillSchema(ds, SchemaType.Source, "[Sheet1$]")
    adapter.Fill(ds, "[Sheet1$]")

    Dim excelData As New DataTable
    DataGridView_excel.DataSource = excelData

    excelData = ds.Tables("[Sheet1$]")
    Dim drCurrent As DataRow
    drCurrent = excelData.NewRow
    drCurrent("First Name") = TextBox1.Text
    drCurrent("Last Name") = TextBox2.Text
    drCurrent("Address") = TextBox3.Text
    drCurrent("Number") = TextBox4.Text
    ' more fields go here

    excelData.Rows.Add(drCurrent)
    Dim objCommandBuilder As New OleDb.OleDbCommandBuilder(adapter)
    objCommandBuilder.QuotePrefix = "["
    objCommandBuilder.QuoteSuffix = "]"
    adapter.Update(ds, "[Sheet1$]")
    DataGridView_excel.DataSource = excelData

End Sub

这个excel文件不是我的,但我必须操纵数据而不改变其原始格式