2014-02-26 26 views
1

我有以下代碼來更改數據,現在我實際上想要將所述數據寫回到Access數據庫中的TESTS_TMP表,我該怎麼做?我認爲適配器更新做到了,但它沒有?寫入DataSet已更改回Access數據庫

Dim dataSet As DataSet = New DataSet 

    Using connection As New OleDbConnection(FileLocations.connectionStringNewDb) 
     connection.Open() 
     Dim adapter As New OleDbDataAdapter() 

     adapter.SelectCommand = New OleDbCommand("SELECT * FROM TESTS_TMP;", connection) 

     Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter) 

     adapter.Fill(dataSet) 

     'MsgBox(dataSet.Tables(0).Rows.Count) 

     'Code to modify the data in the DataSet here. 
     Dim id As Integer = 300 

     For i As Integer = 0 To dataSet.Tables(0).Rows.Count - 1 
      dataSet.Tables(0).Rows(i).Item(0) = id 
      id = id + 1 
     Next 

     ' Without the OleDbCommandBuilder this line would fail. 
     'builder.GetUpdateCommand() 
     'adapter.Update(dataSet) 

     Try 
      adapter.Update(dataSet.Tables(0)) 
      dataSet.AcceptChanges() 

     Catch x As Exception 
      ' Error during Update, add code to locate error, reconcile 
      ' and try to update again. 
     End Try 

    End Using 

    MsgBox(dataSet.Tables(0).Rows(1).Item(0)) 

回答

1

你打電話AcceptChanges。並且它將所有行標記爲未修改,因此它們從不在數據庫中更新。刪除此通話。

+0

我試過刪除,不做任何事情,訪問數據庫中的表仍然不包含更新的數據。 –

+0

您是否在'\ bin \ debug'文件夾中檢查了您的數據庫副本? – equisde

+0

哦,我看到了...我沒有使用數據集的本地副本我只是直接連接到數據庫。我只是想直接對數據庫進行上述操作? –