表属性破坏后C#键入DataSet Copy() [英] Table Property broken after C# Typed DataSet Copy()

查看:137
本文介绍了表属性破坏后C#键入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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆