ADO.NET - adding a DataTable to multiple DataSets

I would like to add a DataTable to multiple DataSets without calling DataTable.Copy (). Simply copying the DataTable doubles the amount of data in memory and forces me to manage changes to it. This issue exists because I am implementing a detail tables solution where the detail table can contain more than one master.

If that helps, consider this snippet and the resulting fake 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 there? Thank!

+2


source to share


2 answers


You cannot do this without copying, because DataSets are mobile-friendly and independently updatable, as a result they must contain the data itself, not just a link to the data ...



+1


source


The class DataTable

has a property DataSet

, which means it was specifically designed to be used by (at most) 1 DataSet. Similarly, a DataRow

has the property Table

.

A DataTable

has properties ChildRelations

and ParentRelations

. They would be ambiguous if a Datatable could belong to more than 1 DataSet. Because the collection of all relationships is a DataSet and the relationship names only apply to be unique in 1 DataSet.



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

+3


source







All Articles