views:

167

answers:

2

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(table1);
dataset2.Tables.Add(table2); // fails because table2 already exists in dataset1 :-(

Any pro suggestions out there? Thanks!

+1  A: 

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...

Charles Bretana
Sorry, that makes no sense since DataTables are references types. And so are the Rows.
Henk Holterman
Henk... My use of the word reference has nothing to do with the .Net distinction between reference types and value types. In this context, technically, DataSets themselves are not references.... even though the variable that holds the dataset, or points to it is a reference variable, and a dataset is a reference Type,... The DataSet itself (the thing on the heap) is not a "reference" to the data in the database, it is a copy of, and includes within it all that data.
Charles Bretana
+3  A: 

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.

Henk Holterman