Vb.net 在一个数据集中使用多个数据表在Access DB中插入新记录
VB2010我有一个数据集,我添加了多个表,然后填写这些表,然后将这些记录插入Access数据库Vb.net 在一个数据集中使用多个数据表在Access DB中插入新记录,vb.net,ms-access,datatable,dataset,oledb,Vb.net,Ms Access,Datatable,Dataset,Oledb,VB2010我有一个数据集,我添加了多个表,然后填写这些表,然后将这些记录插入Access数据库 '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 A
'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.OledBeException”的第一次意外异常
System.Data.OleDb.OLEDBEException(0x80040E14):INSERT INTO语句中出现语法错误。
位于System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs RowUpdateEvent,BatchCommandInfo[]batchCommands,Int32 commandCount)
位于System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdateDevenTargets RowUpdateEvent,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(数据集数据集,字符串srcTable)
我看了很多文章,他们都建议用一个包含多个数据表的数据集更新数据库是可行的,但我就是不明白为什么会这样。您在哪里设置
插入命令
和数据适配器的更新命令
属性
从错误中可以看出,dataadapter
试图将记录插入数据库,但insertcommand
没有实例化或给定值,因此引发了错误
由于数据适配器通过检查datatablesrowstate
中的每一行来决定在.Update
调用上执行什么操作,因此添加到表中的任何行都将尝试使用附加到数据适配器的insertcommand
请尝试在数据适配器上设置插入命令
和更新命令
,然后再次尝试运行,看看会发生什么情况。我希望将在注释中发现的所有内容作为答案
尽量避免在表中使用保留关键字的字段(也包括嵌入空格的字段)
CommandBuilders需要主键信息,因此添加一个adapter.MissingSchemaAction=MissingSchemaAction.AddWithKey
,以从db架构恢复主键信息
使用与填充相同的更新连接
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")
很难说发生了什么事。我将尝试查看daCars适配器上insert命令的命令文本daCars.InsertCommand.CommandText
所以我在daTrips.Update之前添加了一个debug.Print(daTrips.InsertCommand.CommandText),并通过一个异常System.NullReferenceException。UpdateCommand也是这样。我根本就没有设置这些。我以为OleDbCommandBuilder会为您设置这些字段。Cars表只包含上面的两个字段,还是还有其他字段?如果存在,您能显示这些其他字段的名称吗?另外,请尝试使用以下命令-daCars.InsertCommand=cbCars.GetInsertCommand()代码>查看InsertCommand.CommandTextCars表是否定义了主键?如果希望commandbuilder处理更新,则表必须具有主键。我不设置insertcommand或updatecommand属性。我认为这就是OleDbCommandBuilder的目的。我从未亲自使用过命令生成器,我更喜欢手动设置命令,这样我就知道它在做什么。如果命令生成器就是这么做的,我会感到惊讶,因为它怎么知道数据库的模式?不管是哪种方式,如果您设置了insert和update命令并重试,它应该可以正常工作。我还发现字段中的非标准字符会导致更新失败。您已经提到了空格,但我的几个字段有一个“/”,这也有问题。再次感谢。对于一些(不是全部)字符,您可以通过用方括号括起字段名来绕开它:[]。但是,您需要显式地设置命令文本以适应字符。