DataTable without Dataset

  • Thread starter Thread starter C P
  • Start date Start date
C

C P

I've got a class (MyClass) that has a DataSet (privateDataSet) as a private
property. I'd like to give consumers of MyClass read-only access to a
couple of the DataTables in the privateDataSet, as DataTables. What I'm
thinking of doing is creating a public property MyClass.MyDataTable that
returns a clone of a DataTable in privateDataSet. That way if the user
messes around with MyClass.MyDataTable, they won't be changing any of the
data in the underlying DataTable.

However, I see that DataTable has a readonly property 'DataSet' which I
assume will point back to the DataSet that I'm trying to keep hidden. If I
call "MyClass.MyDataTable.DataSet.Clear()", will that get rid of the DataSet
associated with my MyClass.MyDataTable, without affecting the underlying
privateDataSet? Will my cloned DataTable in MyClass.MyDataTable still have
data when it's corresponding DataSet has been cleared?

If what I'm talking about above won't work, is there a way to expose a clone
of a DataTable, without including the original DataTable's Dataset?

Thanks,
Chris
 
Hi CP,

As far as I know is it as this.

The the dataset.clear removes all the rows from every table that is
connected to it.
However there is also a dataset remove table.
That keeps the table (when you have a reference to it) in tact when you have
done that before.

Cor
 
In DataSet/DataTable world:
----
Clone means copying just the schema
Copy means copying both the schema and data
----
dataSet.Copy() - Copies all the tables and the dataset.
dataTable.Copy() copies just the single table. So the copied DataTable's
DataSet property would be "null" since it is just a stand-alond table.

One thing to keep in mind is when you've expression columns[that have
dependency on other tables in the dataset], dataTable.Copy will throw
exception because there is no-way to create a stand-alone table without
copying all the related tables. For these cases, dataSet.Copy() is the right
thing to do, since all the tables get copied.

HTH,
Ravi
 
One thing to keep in mind is when you've expression columns[that have
dependency on other tables in the dataset], dataTable.Copy will throw
exception because there is no-way to create a stand-alone table without
copying all the related tables. For these cases, dataSet.Copy() is the right
thing to do, since all the tables get copied.

If the dataset is big (say it has another table with many rows), this wastes
memory when all you want is one table's data. Could also DataSet.Copy, turn
EnforceConstraints=false, then bring the data over for the one table of
interest.

Brad Williams
 
Setting EnforceConstraints to false will not have an affect on the
"expression" columns. Say if you have Table1, Table2 and if Table1 has an
expression column like Avg(Child(Rel).Qty). Qty is the column in Table2 and
you are trying to copy only Table1, you will have a problem. Table1 cannot
exist without Table2 because of its dependency on Table2's column. Hence
when you try to copy just Table1, you'll see an exception. In just this
case, you'll need to copy the whole dataset rather than just one table.

Thanks,
Ravi

Brad Williams said:
One thing to keep in mind is when you've expression columns[that have
dependency on other tables in the dataset], dataTable.Copy will throw
exception because there is no-way to create a stand-alone table without
copying all the related tables. For these cases, dataSet.Copy() is the right
thing to do, since all the tables get copied.

If the dataset is big (say it has another table with many rows), this wastes
memory when all you want is one table's data. Could also DataSet.Copy, turn
EnforceConstraints=false, then bring the data over for the one table of
interest.

Brad Williams
 
Back
Top