0

VB2010 1 つの DataSet があり、複数のテーブルを追加し、これらのテーブルに入力してから、それらのレコードを 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 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")

最初の更新は機能しますが、2 回目の更新で例外が発生します。

System.Data.dll System.Data.OleDb.OleDbException (0x80040E14): INSERT INTO ステートメントで構文エラーが発生しました。System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors (RowUpdatedEventArgs rowUpdatedEvent、BatchCommandInfo[] batchCommands、Int32 commandCount) で System.Data.Common.DbDataAdapter.UpdatedRowStatus (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 を含む 1 つの DataSet でデータベースを更新することを提案していますが、なぜこれが爆撃なのか理解できません。

4

2 に答える 2

0

発見したことはすべてコメントに答えたい

  1. テーブルで予約済みのキーワードを使用するフィールド(スペースが埋め込まれているフィールドも)を避けるようにしてください
  2. CommandBuildersには主キー情報が必要なのでadapter.MissingSchemaAction = MissingSchemaAction.AddWithKey、データベーススキーマから主キー情報を回復するためにを追加します
  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 に答える
0

insertcommandとのupdatecommandプロパティをどこに設定していますdataadapterか?

dataadapterエラーから、はデータベースにレコードを挿入しようとしているように見えますが、insertcommandインスタンス化されていないか、値が指定されていないため、エラーがスローされます。

.Updateデータアダプタはデータテーブルの各行を調べて呼び出しで何をするかを決定するrowstateため、テーブルに追加する行はすべて、に接続されている行を使用しようとinsertcommandしますdataadapter

insertcommandとを設定して、updatecommandもう一度dataadapters実行してみて、そこから何が起こるかを確認してください。

于 2012-06-29T20:55:27.327 に答える