McArthey
McArthey

Reputation: 1646

Strongly typed dataset using variables for column names

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!

Upvotes: 0

Views: 1488

Answers (2)

Mark Ewer
Mark Ewer

Reputation: 1835

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.

Upvotes: 0

recursive
recursive

Reputation: 86164

You could use a dynamic type to get you part of the way there, but your goal sounds kind of contradictory.

dynamic Dr = new ExpandoObject();
Dr.whatever = 6;
Dr.anything = "asdf";

If you use ExpandoObject with dynamic, you can assign any property.

Upvotes: 3

Related Questions