【发布时间】:2013-05-11 12:58:56
【问题描述】:
我已经非常努力地解决了我的问题,这就是我想要做的:
- 我有一个 XML 文件,我使用
ReadXML将其加载到数据集(“ds”)中, 几个表被填充到数据集中,我关心的那个 about is ("SalesReceiptRet") 我将其称为源 表。 - 我在 MS Access 数据库中有另一个表,我将它加载到同一个表中
使用OleDBAdapter.Fill将数据集放入名为 ("dtTarget") 的数据表中 并且适配器的名称是 ("dbAdapter")。
我想遍历源表中的所有记录,查找一个名为(“TxnID”)的字段来定位目标表中的记录。如果它不存在,则添加它,如果它确实执行另一个验证并使用源行覆盖/更新它。
代码如下:
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
'Initialize command builder
Dim dbCommandBuilder As New OleDb.OleDbCommandBuilder(dbAdapter)
'Set the primary keys
ds.Tables("dtTarget").PrimaryKey = New DataColumn() {ds.Tables("dtTarget").Columns("TxnID")}
'Find rows
Dim dr As DataRow
Dim lookupRow As DataRow
For Each dr In ds.Tables("SalesReceiptRet").Rows
lookupRow = ds.Tables("dtTarget").Rows.Find(dr.Item(0))
'If the a row with a similar TxnID exists, do the following validation
If Not (lookupRow Is Nothing) Then
If lookupRow.Item(8).ToString <> "Regular" Then
'do something here to overwrite/update the matching row
End If
Else
'If the row does not exist, import it
ds.Tables("dtTarget").ImportRow(dr)
End If
Next
'Update Access
dbAdapter.Update(ds, "dtTarget")
dbConnection.Close()
End Sub
也许我需要提一下,这两个表都有确切的列名,除了 Access“dtTarget”还有其他的,这似乎至少不会导致导入行出现问题。
有什么想法吗?
【问题讨论】:
-
你的代码现在在做什么?不是在更新吗?不是在进口吗?是不是报错了?
-
它可以正常导入但没有更新,因为我还没有为此写任何东西