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 来更新数据库是可行的,但只是无法弄清楚为什么这是轰炸。