从一个数据集复制到另一个数据表 [英] Copy DataTable from one DataSet to another
问题描述
我想添加到一个新的DataSet X中的数据表这是一个不同的数据集Y.
里面如果我直接添加它,我收到以下错误:
I'm trying to add to a new DataSet X a DataTable that is inside of a different DataSet Y. If I add it directly, I get the following error:
的 DataTable中已属于另一个数据集。的
我一定要克隆的DataTable,并导入所有的行,然后将新的DataTable添加到新的数据集? ?有没有更好的/简单的方法来做到这一点。
Do I have to clone the DataTable and import all the rows to it and then add the new DataTable to the new DataSet? Is there a better/easy way to do it?
推荐答案
有两个简单的方法可以做到这一点:
There are two easy ways to do this:
代替的 DataTable.Clone
,使用的 DataTable.Copy
来创建数据表的副本;然后将复制到目标的DataSet
:
Instead of DataTable.Clone
, use DataTable.Copy
to create a copy of your data table; then insert the copy into the target DataSet
:
dataSetX.Tables.Add( dataTableFromDataSetY.Copy() );
DataSet.Merge
您也可以使用 的DataSet .Merge
此:
dataSetX.Merge(dataTableFromDataSetY);
请注意,但是,如果你要使用这个方法,你可能想确保你的目标的DataSet
尚未包含具有相同名称的表:
Note, however, that if you are going to use this method, you might want to make sure that your target DataSet
doesn't already contain a table with the same name:
-
如果目标DataSet中不包含表按相同的名称,是数据集内创建表的新副本;
If the target DataSet doesn't contain a table by the same name, a fresh copy of the table is created inside the data set;
如果由同名的表已在目标数据集,然后它会被与一个传递到合并
合并,你最终是两者的混合。
If a table by the same name is already in the target data set, then it will get merged with the one passed to Merge
, and you end up with a mix of the two.
这篇关于从一个数据集复制到另一个数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!