Write DataSet已更改回Access数据库

时间:2014-02-26 12:15:35

标签: vb.net dataset

我有以下代码来更改数据,现在我实际上想将所述数据写回Access数据库中的TESTS_TMP表,我该怎么做?我认为适配器更新确实如此,但事实并非如此?

    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 个答案:

答案 0 :(得分:1)

您正在呼叫AcceptChanges。并且它将所有行标记为未修改,因此它们永远不会在数据库中更新。删除此电话。