bradjive
bradjive

Reputation: 1492

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

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!

Upvotes: 2

Views: 748

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273621

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.

Upvotes: 3

Charles Bretana
Charles Bretana

Reputation: 146557

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

Upvotes: 1

Related Questions