Write DataSet changed back to Access database

我与影子孤独终老i 提交于 2020-01-05 04:51:21

问题


I have the following code to alter the data, now I actually want to write said data back to the TESTS_TMP table in the Access database, how do I do that? I thought the adapter update did that but it doesn't?

    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:


You're calling AcceptChanges. and it is marking all of the rows as being unmodified, so they are never updated in the database. Remove this call.



来源:https://stackoverflow.com/questions/22041080/write-dataset-changed-back-to-access-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!