使用变量作为列名的强类型数据集
我目前正在通过索引引用通用数据集,并希望将其转换为表类型的强类型数据集。问题是我的 DataRow 索引是一个变量。
目前,我正在执行以下操作,其中从数据库中提取 History_Column 值。
<代码> e.Dr[c.History_Column.ToString()] = 条目;
我想将 DataRow(示例中的“Dr”)定义为表的类型,以便我可以执行类似于以下操作的操作:
e.Dr.COLUMN_NAME = 条目;
如何以这种方式使用动态变量?
谢谢!
I am currently referencing a generic DataSet via an index and would like to convert this to a strongly-typed DataSet of the table type. The issue is that my index to the DataRow is a variable.
Currently I am doing the following where the History_Column value is pulled from the database.
e.Dr[c.History_Column.ToString()] = Entry;
I would like to define the DataRow ('Dr' in the example) as a type of the table so I can do something similar to the following:
e.Dr.COLUMN_NAME = Entry;
How can I use dynamic variables in this fashion?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用动态类型来实现这一目标,但您的目标听起来有点矛盾。
如果将
ExpandoObject
与dynamic
一起使用,则可以分配任何属性。You could use a dynamic type to get you part of the way there, but your goal sounds kind of contradictory.
If you use
ExpandoObject
withdynamic
, you can assign any property.强类型数据集是应用程序中派生自内置数据集类的类。您必须将它们添加到 Visual Studio 中的项目中。以下是创建强类型数据集的一些说明。
但是,我建议您借此机会进行重构并完全摆脱数据集(如果可以的话)。 实体框架为每列提供强类型属性访问器,这是一个更好的“单元” -of-work”模式,一个图形化的数据库映射工具,而且使用起来更加方便。
A strongly-typed dataset is a class in your application that derives from the built-in dataset class. You have to add them to your project in visual studio. Here are some directions for creating strongly typed datasets.
However, I suggest that you take the opportunity to refactor and get away from datasets altogether (if you can). Entity Framework provides strongly typed property accessors for each column, a better "unit-of-work" pattern, a graphical database mapping tool, and it is much easier to use.