tags:

views:

2680

answers:

3

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.

+2  A: 

What is wrong with DataTable.Copy?

Geoffrey Chetwood
I need to explain a bit more. There is a filtering condition for copying the rows. So, a plain Copy() will not work.
Alex
+4  A: 

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."

Eugene Katz
It is not exactly a clone. I guess manually iterating through the datatable is the only option..
Alex
+2  A: 

Copying rows to a table throws some flags at me. I've seen people try this before, and in every single case what they really wanted was a System.Data.DataView. You really should check to see if the RowFilter property will do what you need it to do.

Alex Lyman