Best way to merge two Datatables
datatable, vb.net
Solution
Not sure if can be applicable, but have you ever looked at the DataTable.LoadDataRow() method?. It seems a good candidate to substitute all of you code above.
Your code could be simplified to these lines
Dim row as DataRow
For Each row in RemoteTable.Rows
LocalTable.LoadDataRow(row.ItemArray, false)
Next
Another alternative could be the DataTable.Merge that could cut your code to a single line
LocalTable.Merge(RemoteTable, False)
However, the real effectiveness of these two methods depends on the schema compatibility and from the presence of AutoNumber (identity) columns.
Problem
I need to marge two datatables with condition. I have a datatable where the data comes from a local XML Database and another datatable where the data comes from a remote SQL Server. If any update made in the remote datatable I need to update/merge with the local datatable. Here is what I have so far: ``` Public Sub MargeTwoTable() Dim SQL As String = "" Dim RemoteTable As New DataTable Dim LocalTable As DataTable Dim dal As New DalComon Dim yy As Integer = 0 Dim UpdateDate As String Dim TableName As String = "V_Book_Price" LocalTable = LoadDataTable(TableName, True) UpdateDate = LocalTable.Compute("MAX(update_date)", Nothing) SQL = "select * from V_Book_Price where Update_Date > '" & UpdateDate & "'" RemoteTable = dal.GetDataSetBySQL(SQL).Tables(0) If RemoteTable.Rows.Count > 0 Then For i = 0 To RemoteTable.Rows.Count - 1 Dim st As DataRow Dim mm() As DataRow = LocalTable.Select("ID = '" & RemoteTable.Rows(i).Item("ID") & "'") If mm.Length = 0 Then st = LocalTable.NewRow For yy = 0 To RemoteTable.Columns.Count - 1 st(yy) = RemoteTable.Rows(i)(yy) Next LocalTable.Rows.Add(st) Else st = mm(0) For yy = 0 To RemoteTable.Columns.Count - 1 If IsDate(RemoteTable.Rows(i)(yy)) Then st(yy) = CDate(RemoteTable.Rows(i)(yy)).ToString("s") Else st(yy) = RemoteTable.Rows(i)(yy) End If Next mm = Nothing End If Next End If End Sub ``` In this code data comes from the remote database which updates a date getter then the local database . Both tables have "ID" as the primary key. The code is working well, but the problem is that when more than 1000 records are updated this function takes too long using loops.