Append 1 DataTable to another

D

Doug Bell

Hi,
Is there a more efficient way to append the rows of a DataTable to a second
DataTable that has the same structure?

Currently I am using a For Next loop:

Dim drSource, drTarget as DataRow
For Each drSource in MySourceDataTable
drTarget =MyTargetDataTable.Rows.NewRow
drTarget = drSource
MyTargetDataTable.Rows.Add(drTarget )
Next dr

Thanks
Doug
 
J

Jay B. Harlow [MVP - Outlook]

Doug,
You can simplify your routine by using ImportRow instead of Rows.Add.

Something like:

For Each drSource As DataRow in MySourceDataTable
MyTargetDataTable.ImportRow(drSource)
Next dr

Note: A potential problem with your code:
drTarget =MyTargetDataTable.Rows.NewRow
Creates a new data row.
drTarget = drSource
Discards the newly created row in favor of the existing row.

Review the on-line help for differences between ImportRow & NewRow.

Hope this helps
Jay
 
D

Doug Bell

Thanks Jay,

That is much better.

It does, however, still need to loop through each row. Is there any way to
do a bulk import of the new rows?

Doug
 
J

Jay B. Harlow [MVP - Outlook]

Doug,
No, as Cor stated you can use DataTable.Copy to create an entire copy of a
DataTable (structure & data).

For a complete explanation of advanced features of Datasets, such as copying
& importing, I would strongly recommend you read David Sceppa's book
"Microsoft ADO.NET - Core Reference" from MS Press. As it is a good tutorial
on ADO.NET as well as a good desk reference once you know ADO.NET.

Hope this helps
Jay
 
D

Doug Bell

Jay,
Thanks for the tip, I will see if I can find that Book.

I have a Evangelos Petroutsos book that has helped quite a bit. We have
moved development to Dot Net but "we" did not factor in any get up to speed
time and I have found it a challenge but can see it is going to be very
worthwhile.

Current small project was meant to be deployed by the end of November! A
graph of progress would look like a yoyo.

Again thanks for you assistance.

Doug
 

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