将DataSet更改回Access数据库 [英] Write DataSet changed back to Access database

查看:166
本文介绍了将DataSet更改回Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来修改数据,现在我实际上想将所述数据写回Access数据库中的 TESTS_TMP 表,我该怎么做?我认为适配器更新是这样做的,但它不是?

  Dim dataSet As DataSet = New DataSet 

使用连接作为新的OleDbConnection(FileLocations.connectionStringNewDb)
connection.Open()
Dim适配器作为新的OleDbDataAdapter()

adapter.SelectCommand =新的OleDbCommand(SELECT * FROM TESTS_TMP;,连接)

Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)

adapter.Fill(dataSet)

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

'在这里修改DataSet中的数据的代码。
Dim id As Integer = 300

对于i As Integer = 0到dataSet.Tables(0).Rows.Count - 1
dataSet.Tables(0).Rows i).Item(0)= id
id = id + 1
下一个

'没有OleDbCommandBuilder这一行将失败。
'builder.GetUpdateCommand()
'adapter.Update(dataSet)

尝试
adapter.Update(dataSet.Tables(0))
dataSet .AcceptChanges()

抓取x作为异常
'更新时出错,添加代码以找到错误,调整
'并尝试再次更新。
结束尝试

结束使用

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


解决方案

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


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))

解决方案

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.

这篇关于将DataSet更改回Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆