0

VB2010 我有一个数据集,我添加了多个表,然后我填写这些表,然后将这些记录插入一个 Access db。

    'create a new DataSet
    Dim dsNav As New DataSet

    'first table
    Dim daTrips As New OleDb.OleDbDataAdapter("SELECT * FROM Trips", connNav)
    daTrips.Fill(dsNav, "Trips")
    Dim cbTrips As New OleDb.OleDbCommandBuilder(daTrips)

   'second table
    Dim daCars = New OleDb.OleDbDataAdapter("SELECT * FROM Cars", connNavDb)
    daCars.Fill(dsNav, "Cars")
    Dim cbCars As New OleDb.OleDbCommandBuilder(daCars)

    'here i open a huge text file and depending on the data i encounter, I create
    'a new DataRow and add it to the appropriate table. i add many new rows to each
    'table. for example
    Dim dsNewRow As DataRow = {tblCars}.NewRow()
    dsNewRow.Item("CarId") = textline.Substring(0, 10)
    dsNewRow.Item("CarMake") = textline.Substring(11, 15)
    tblCars.Rows.Add(dsNewRow)

    'i finish reading the text file and filling up the tables in the one DataSet
    'now i want to insert those records into the Access db
    Dim rowCnt1 As Integer = daTrips.Update(dsNav, "Trips")
    Dim rowCnt2 As Integer = daCars.Update(dsNav, "Cars")

第一次更新有效,但在第二次更新时出现异常:

System.Data.dll System.Data.OleDb.OleDbException (0x80040E14) 中出现“System.Data.OleDb.OleDbException”类型的第一次机会异常:INSERT INTO 语句中的语法错误。在 System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) 在 System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) 在 System.Data.Common .DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) 在 System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping) 在 System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)

我看过各种文章,他们都建议用一个包含多个 DataTables 的 DataSet 来更新数据库是可行的,但只是无法弄清楚为什么这是轰炸。

4

2 回答 2

0

你在哪里设置你的insertcommandupdatecommand属性dataadapter

从您的错误看来,它dataadapter正在尝试将记录插入数据库,但insertcommand没有实例化或没有给出值,因此它抛出了错误。

由于数据适配器通过检查数据表中的每一行来决定在.Update调用中执行什么操作,rowstate因此您添加到表中的任何行都将尝试使用insertcommand附加到您的dataadapter.

尝试在您的设备上设置insertcommandand并再次尝试运行它,看看会发生什么。updatecommanddataadapters

于 2012-06-29T20:55:27.327 回答
0

我希望将在评论中发现的所有内容作为答案

  1. 尽量避免在表格中使用保留关键字的字段(以及嵌入空格的字段)
  2. CommandBuilders 需要主键信息,因此添加一个adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey以从 db 模式中恢复主键信息
  3. 对更新使用与填充相同的连接

    Dim dsNav As New DataSet 
    'first table 
    Dim daTrips As New OleDb.OleDbDataAdapter("SELECT * FROM Trips", connNav) 
    daTrips.MissingSchemaAction = MissingSchemaAction.AddWithKey 
    daTrips.Fill(dsNav, "Trips") 
    Dim cbTrips As New OleDb.OleDbCommandBuilder(daTrips) 
    
    'second table 
    Dim daCars = New OleDb.OleDbDataAdapter("SELECT * FROM Cars", connNav) 
    daCars.MissingSchemaAction = MissingSchemaAction.AddWithKey 
    daCars.Fill(dsNav, "Cars") 
    Dim cbCars As New OleDb.OleDbCommandBuilder(daCars) 
    
    'here i open a huge text file and depending on the data i encounter, I create 
    'a new DataRow and add it to the appropriate table. i add many new rows to each 
    'table. for example 
    Dim dsNewRow As DataRow = {tblCars}.NewRow() 
    dsNewRow.Item("CarId") = textline.Substring(0, 10) 
    dsNewRow.Item("CarMake") = textline.Substring(11, 15) 
    tblCars.Rows.Add(dsNewRow) 
    
    'i finish reading the text file and filling up the tables in the one DataSet 
    'now i want to insert those records into the Access db 
    Dim rowCnt1 As Integer = daTrips.Update(dsNav, "Trips") 
    Dim rowCnt2 As Integer = daCars.Update(dsNav, "Cars") 
    
于 2012-07-01T20:20:39.480 回答