Table Property broken after C# Typed DataSet Copy()

半腔热情 提交于 2019-12-13 04:32:42

问题


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();


来源:https://stackoverflow.com/questions/6756746/table-property-broken-after-c-sharp-typed-dataset-copy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!