开发者

ADO .NET - adding a DataTable to more than one DataSet

开发者 https://www.devze.com 2022-12-09 10:18 出处:网络
I\'d like to add a DataTable to more than one DataSet without calling DataTable.Copy(). Simply copying the DataTable doubles the amount of data in memory and causes me to manage its changes. This prob

I'd like to add a DataTable to more than one DataSet without calling DataTable.Copy(). Simply copying the DataTable doubles the amount of data in memory and causes me to manage its changes. This problem exists because I am implementing a detail tables solution where a detail table can have more than one master.

If it helps, consider this snippet and resulting frowny face.

DataTable table1 = GetDataTable(); // 100 rows of data
DataTable table2 = table1;

DataSet1 dataset1 = new DataSet();
DataSet2 dataset2 = new DataSet();

dataset1.Tables.Add(t开发者_C百科able1);
dataset2.Tables.Add(table2); // fails because table2 already exists in dataset1 :-(

Any pro suggestions out there? Thanks!


The DataTable class has a DataSet property, that means it was intentionally designed to belong to (at most) 1 DataSet. Likewise, a DataRow has a Table property.

A DataTable has ChildRelations and ParentRelations properties. They would be ambiguous if a Datatable could belong to more than 1 DataSet. Because the collection of all Relations belongs to the DataSet, and the relation-names are only enforced to be unique in 1 DataSet.

There are probably more (and better) reasons why a Table cannot be shared between DataSets.


You can't do this without copying because DataSets are designed to be mobile, and independantly updatable, as a result, they must contain the data itself, not just a reference to the data...

0

精彩评论

暂无评论...
验证码 换一张
取 消