C# 将数据从一个 DataTable 复制到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/67929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying data from one DataTable to another
提问by Alex
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() 将不起作用。
采纳答案by Eugene Katz
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类的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 表复制到另一个在内存中创建的表。”
回答by GEOCHET
What is wrong with DataTable.Copy?
DataTable.Copy 有什么问题?
回答by Alex Lyman
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 RowFilterproperty will do what you need it to do.
将行复制到表会向我抛出一些标志。我以前见过人们尝试过,在每种情况下,他们真正想要的是System.Data.DataView。你真的应该检查一下RowFilter属性是否会做你需要它做的事情。