Table Property broken after C# Typed DataSet Copy(

2019-08-28 06:37发布

问题:

I've got a typed DataSet with multiple tables that I want copy, complete with data as well as schema. I can access the tables like so in my original:

MyDataSetType dsMyFirstDataSet;
MyDataTableType dtTable1;
MyDataTableType dtTable2;

dtTable1 = dsMyFirstDataSet.MeaningfulTableName1;
dtTable2 = dsMyFirstDataSet.MeaningfulTableName2;

When I do the following, I can no longer access the tables via the names in the new version but I can via the Tables collection.

MyDataSetType dsMySecondDataSet; 
dsMySecondDataSet = dsMyFirstDataSet.Copy();

dtTable1 = dsMySecondDataSet.MeaningfulTableName1;  // null
dtTable2 = dsMySecondDataSet.MeaningfulTableName2;  // null

dtTable1 = dsMySecondDataSet.Tables[0];  // table not null/copied ok
dtTable2 = dsMySecondDataSet.Tables[1];  // table not null/copied ok

I'm currently accessing them via the index like the second example but I'm wondering why the named table link has been severed? Do I have to write my own Copy() method to preserve the named access to the tables?

回答1:

My guess is that your call to Copy is returning a non-typed DataSet (though I'm not sure why you wouldn't get a compiler error).

Try changing the line where you call Copy to this:

dsMySecondDataSet = (MyDataSetType) dsMyFirstDataSet.Copy();