简体   繁体   English

C#DataTable:ImportRow(DataRow)与Rows.Add(DataRow.ItemArray)

[英]C# DataTable: ImportRow(DataRow) vs. Rows.Add(DataRow.ItemArray)

I have two DataTable with the same schema: 我有两个具有相同架构的DataTable

DataTable t1 = new DataTable();
DataTable t2 = t1.Clone();

If I want to add a new DataRow to t1 : 如果我想向t1添加新的DataRow

DataRow row=t1.NewRow();
t1.Rows.Add(t1);

I cannot use t1.ImportRow(row); 我不能使用t1.ImportRow(row); since after been created, row is in a detached state, and therefore it would be ignored by the ImportRow method (see Microsoft documentation ). 由于创建后,该row处于分离状态,因此ImportRow方法将忽略该行(请参阅Microsoft文档 )。

Now I want to copy row into t2 : I know that a DataRow object can be contained only in one DataTable , therefore one needs to create a copy, to do it one can use two methods: 现在,我要将row复制到t2 :我知道一个DataRow对象只能包含在一个DataTable ,因此需要创建一个副本,为此可以使用两种方法:

t1.Rows.Add(row.ItemArray);

or 要么

t1.ImportRow(row);

My question is: are t1.Rows.Add(row.ItemArray); 我的问题是:是t1.Rows.Add(row.ItemArray); and t1.ImportRow(row); t1.ImportRow(row); equivalent? 当量? Are all my assumptions correct? 我所有的假设都正确吗?

I've looked into the code and it looks like t1.Rows.Add(row.ItemArray); 我已经看过代码,看起来像t1.Rows.Add(row.ItemArray); would be a little less complex. 会稍微复杂一点。 Anyway the methods are not equivalent technicaly. 无论如何,这些方法在技术上并不等效。

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

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