Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying data from one DataTable to another

Tags:

c#

ado.net

What is the fastest way of transferring few thousand rows of data from one DataTable to another? Would be great to see some sample code snippets.

Edit: I need to explain a bit more. There is a filtering condition for copying the rows. So, a plain Copy() will not work.

like image 883
Alex Avatar asked Sep 15 '08 23:09

Alex


1 Answers

You can't copy the whole table, you need to copy one rows. From http://support.microsoft.com/kb/308909 (sample code if you follow the link)

"How to Copy DataRows Between DataTables Before you use the ImportRow method, you must ensure that the target table has the identical structure as the source table. This sample uses the Clone method of DataTable class to copy the structure of the DataTable, including all DataTable schemas, relations, and constraints.

This sample uses the Products table that is included with the Microsoft SQL Server Northwind database. The first five rows are copied from the Products table to another table that is created in memory."

like image 88
Eugene Katz Avatar answered Sep 28 '22 08:09

Eugene Katz