C# DataTable: ImportRow(DataRow) vs. Rows.Add(DataRow.ItemAr
本文关键字:DataRow Rows Add ItemAr vs ImportRow DataTable | 更新日期: 2023-09-27 18:37:07
我有两个具有相同架构的DataTable
:
DataTable t1 = new DataTable();
DataTable t2 = t1.Clone();
如果我想向t1
添加新DataRow
:
DataRow row=t1.NewRow();
t1.Rows.Add(t1);
我无法使用t1.ImportRow(row);
,因为在创建后,row
处于分离状态,因此ImportRow
方法会忽略它(请参阅Microsoft文档)。
现在我想将row
复制到t2
中:我知道一个DataRow
对象只能包含在一个DataTable
中,因此需要创建一个副本,为此可以使用两种方法:
t1.Rows.Add(row.ItemArray);
或
t1.ImportRow(row);
我的问题是:t1.Rows.Add(row.ItemArray);
和t1.ImportRow(row);
等价吗?我所有的假设都是正确的吗?
我已经查看了代码,它看起来像t1.Rows.Add(row.ItemArray);
会不那么复杂一些。无论如何,这些方法在技术上并不等同。