Merge DataTable Rows into Second DataTable

G

Guest

All,

Can anyone help me understand the best way to copy all rows from an existing
DataTable into a second DataTable which also has existing rows. The following
code takes 6-7 seconds for 1200 rows in sourceTable and 1 or more rows in
destinationTable. If there are 0 rows in destinationTable, the code runs in 1
second or less. How can I get the run time down when there are rows in
destinationTable?

TIA,
-- MN.

Dim sourceTable As DataTable
Dim destinationTable As DataTable
Dim existingRow As DataRow
destinationTable.BeginLoadData()
For Each existingRow In sourceTable.Rows
newRow(0) = existingRow(0)
newRow(1) = existingRow(1)
newRow(2) = existingRow(2)
destinationTable.LoadDataRow(newRow, False)
Next
sourceTable.Clear()
destinationTable.EndLoadData()
destinationTable.AcceptChanges()
 
G

Guest

Assuming you have similar data structures, you can merge the DataTables using
the DataSet's Merge method, like this:

// Create a DataSet so I can merge the DataTable objects
DataSet oDs = new DataSet("MyFunDataSet");
oDs.Tables.Add(sourceTable);
oDs.Merge(destinationTable);

The Merge method will add the rows to the destimation table if it does not
find a matching primary key.

// John Papa
// http://codebetter.com/blogs/john.papa
 
G

Guest

Perfect, thanks.

John Papa said:
Assuming you have similar data structures, you can merge the DataTables using
the DataSet's Merge method, like this:

// Create a DataSet so I can merge the DataTable objects
DataSet oDs = new DataSet("MyFunDataSet");
oDs.Tables.Add(sourceTable);
oDs.Merge(destinationTable);

The Merge method will add the rows to the destimation table if it does not
find a matching primary key.

// John Papa
// http://codebetter.com/blogs/john.papa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top