简体   繁体   English

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

[英]Copying data from one DataTable to another

What is the fastest way of transferring few thousand rows of data from one DataTable to another?将几千行数据从一个 DataTable 传输到另一个 DataTable 的最快方法是什么? 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.因此,普通的 Copy() 将不起作用。

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. 《如何在DataTables之间复制DataRows 在使用ImportRow方法之前,必须确保目标表与源表结构一致。本示例使用DataTable class的Clone方法复制DataTable的结构,包括所有DataTable模式、关系和约束。

This sample uses the Products table that is included with the Microsoft SQL Server Northwind database.此示例使用 Microsoft SQL Server Northwind 数据库中包含的 Products 表。 The first five rows are copied from the Products table to another table that is created in memory."前五行从 Products 表复制到另一个在 memory 中创建的表。”

What is wrong with DataTable.Copy ? DataTable.Copy有什么问题?

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 .我以前见过人们尝试过这个,在每种情况下,他们真正想要的是System.Data.DataView You really should check to see if the RowFilter property will do what you need it to do.您确实应该检查RowFilter属性是否会执行您需要它执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM