表属性破坏后C#键入DataSet Copy() [英] Table Property broken after C# Typed DataSet Copy()
问题描述
MyDataSetType dsMyFirstDataSet;
MyDataTableType dtTable1;
MyDataTableType dtTable2;
dtTable1 = dsMyFirstDataSet.MeaningfulTableName1;
dtTable2 = dsMyFirstDataSet.MeaningfulTableName2;
当我执行以下操作时,我无法再通过新版本的名称访问表,我可以通过Tables集合。
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
我正在通过索引访问它们,如第二个例子但我想知道为什么命名表链接被切断了?我必须编写自己的Copy()方法来保留对表的命名访问吗?
我的猜测是,你的调用复制
返回一个非类型的DataSet(虽然我不知道为什么你不会得到编译器错误)。
尝试更改您调用的行将
复制到此:
dsMySecondDataSet =(MyDataSetType)dsMyFirstDataSet.Copy();
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?
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();
这篇关于表属性破坏后C#键入DataSet Copy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!