将数据从一个DataTable复制到另一个DataTable

时间:2022-09-15 22:28:25

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.

将几千行数据从一个DataTable转移到另一个DataTable的最快方法是什么?很高兴看到一些示例代码片段。

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

编辑:我需要解释一下。复制行有一个过滤条件。因此,普通的Copy()将无法正常工作。

3 个解决方案

#1


7  

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)

你不能复制整个表,你需要复制一行。从http://support.microsoft.com/kb/308909(示例代码,如果您按照链接)

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

“如何在DataTable之间复制DataRows在使用ImportRow方法之前,必须确保目标表具有与源表相同的结构。此示例使用DataTable类的Clone方法复制DataTable的结构,包括所有DataTable模式,关系和约束。

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

此示例使用Microsoft SQL Server Northwind数据库附带的Products表。前五行从Products表复制到另一个在内存中创建的表。

#2


3  

What is wrong with DataTable.Copy?

DataTable.Copy有什么问题?

#3


2  

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.

将行复制到表会向我抛出一些标记。我见过人们之前尝试过这个,并且在每一个案例中他们真正想要的是System.Data.DataView。你真的应该检查一下RowFilter属性是否会完成你需要它做的事情。

#1


7  

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)

你不能复制整个表,你需要复制一行。从http://support.microsoft.com/kb/308909(示例代码,如果您按照链接)

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

“如何在DataTable之间复制DataRows在使用ImportRow方法之前,必须确保目标表具有与源表相同的结构。此示例使用DataTable类的Clone方法复制DataTable的结构,包括所有DataTable模式,关系和约束。

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

此示例使用Microsoft SQL Server Northwind数据库附带的Products表。前五行从Products表复制到另一个在内存中创建的表。

#2


3  

What is wrong with DataTable.Copy?

DataTable.Copy有什么问题?

#3


2  

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.

将行复制到表会向我抛出一些标记。我见过人们之前尝试过这个,并且在每一个案例中他们真正想要的是System.Data.DataView。你真的应该检查一下RowFilter属性是否会完成你需要它做的事情。