簡體   English   中英

更新,將行添加到數據表 VB.Net

[英]Update, Add rows to a datatable VB.Net

我一直在努力解決我的問題,這就是我想要做的:

  • 我有一個 XML 文件,我使用ReadXML將其加載到數據集(“ds”)中,將幾個表填充到數據集中,我關心的是(“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” 有額外的列名,這似乎至少不會對導入行造成問題。

請問有什么想法嗎?

這應該讓你開始,

有關訪問和插入/更新適配器的更多幫助,請參閱此鏈接http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/958e6d51-725a-4649-8fc0-79a831f8f37e

Dim connStr As String = "Provider=SQLNCLI10;Server=(local);Database=MyNewTest;Uid=user; Pwd=******;"
Dim selectCmd As String = "select * from IdentityTest"
Dim dbAdapter As New OleDb.OleDbDataAdapter(selectCmd, connStr)
dbAdapter.AcceptChangesDuringUpdate = True

'code here to fille up your adapter
'build the insert and update comand on the adapter
Dim InsertCommans As New OleDb.OleDbCommand("INSERT INTO <table> (field1,field2,field3,field4) Values (@val1,@val2,@val3,@val4)")
Dim UpdateCommans As New OleDb.OleDbCommand("UPDATE <table> SET <field> = @Value WHERE <field> = @TxnID")

'Set the primary keys
Dim ds As DataSet
ds.Tables("dtTarget").PrimaryKey = New DataColumn() {ds.Tables("dtTarget").Columns("TxnID")}

'assign our datatables
Dim oDtSource As DataTable = ds.Tables("SalesReceiptRet")
Dim oDtTarget As DataTable : dbAdapter.Fill(oDtTarget)

'for each TxnID in our source table
For Each dr As DataRow In oDtSource.Rows

'get the rows in the target that match to the current source table row taxnid [colname] = TxnID
Dim oRowsInTarget As DataRow() = oDtTarget.Select("[" & oDtTarget.Columns(0).ColumnName & "] = " & dr("TxnID"))
'if there is a match (assuming there will only be one) and the column 8 contents (this is column 9 in reality)
'dowes not equal "Regular" then proceed to run the ole adapter update statement else insert
If oRowsInTarget.Count > 0 AndAlso Not oRowsInTarget(0)(8).ToString <> "Regular" Then
    'perform our update
    'you have thematching TxnId in dr("TxnID")
    'I think you can configure an insert and update statment on the Ole adapter connected to access
    'EXEC UPDATE ON THE ADAPTER
ElseIf oRowsInTarget.Count > 0 Then
    'EXEC INSERT ON THE ADAPTER
End If
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM