.net 如何在DataTable中定义DataType列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4379809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:00:35  来源:igfitidea点击:

How to define a DataType column in DataTable?

.netdatatabletypestype-conversion

提问by Sri Reddy

I have a requirement to store the some data in a datatable. The columns in the table are: -ID -Text -Value -DataType How should I define the table columns so that the DataType column stores native and composite types?

我需要将一些数据存储在数据表中。表中的列是: -ID -Text -Value -DataType 我应该如何定义表列,以便 DataType 列存储本机和复合类型?

Currently I am storing it as

目前我将它存储为

dt.Columns.Add(new DataColumn("Id", typeof(int)));
dt.Columns.Add(new DataColumn("Text", typeof(string)));
dt.Columns.Add(new DataColumn("Value", typeof(string)));
dt.Columns.Add(new DataColumn("DataType", typeof(string)));

Later in my program I am trying to parse the values and depending on the datatype I show the control. Like, if the datatype is int then show textbox, if bool then show dropdown.

稍后在我的程序中,我尝试解析值并根据数据类型显示控件。比如,如果数据类型是 int 则显示文本框,如果是 bool 则显示下拉列表。

I am sure there will be an easier way to do this. Please suggest how to achieve this functionality? Example code will help me in understanding it better. Thanks!

我相信会有更简单的方法来做到这一点。请建议如何实现此功能?示例代码将帮助我更好地理解它。谢谢!

回答by Dour High Arch

Creating columns like DataType and Value in a DataColumnor DBMS is usually symptomatic of the Inner-platform effect, where you try to create a typed database inside your typed database by ignoring the type definitions in your database.

在 aDataColumn或 DBMS 中创建 DataType 和 Value 等列通常是内部平台效应的症状,您尝试通过忽略数据库中的类型定义在类型化数据库中创建类型化数据库。

Don't do that. DataColumns already support typed data, querying the schema, and joining tables that contain heterogeneous data. Store ints as ints , booleans as booleans, and you won't have to parse anything. You can determine the data type of a DataColumnwith:

不要那样做。DataColumns 已经支持类型化数据、查询模式和连接包含异构数据的表。将整数存储为 ints ,将布尔值存储为布尔值,您将不必解析任何内容。您可以使用以下命令确定 a 的数据类型DataColumn

var dc = new System.Data.DataColumn("ID", typeof(int));
bool isInt = dc.DataType.Equals(typeof(int));

回答by Tony Abrams

You can make the code a little easier by doing the following:

您可以通过执行以下操作使代码更容易一些:

dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Text", typeof(string));
dt.Columns.Add("Value", typeof(string));
dt.Columns.Add("DataType", typeof(string));

There is no need to use the datacolumn constructor.

无需使用数据列构造函数。

回答by JohnnyJP

So, you are really creating some sort of authoring tool. You define your datatypes in a database and then, create the controls on the fly when then data is read?

因此,您实际上是在创建某种创作工具。您在数据库中定义数据类型,然后在读取数据时动态创建控件?

Ok, so rather than store the default values (true, false) in the database, what about storing them in the controls. For example you now a checkbox must have default values of true/false, A txtbax has default values of blank etc.

好的,与其将默认值(true、false)存储在数据库中,不如将它们存储在控件中。例如,您现在的复选框必须具有 true/false 的默认值,txtbax 的默认值为空白等。

Or, yu could extend your database to hold a list of possible values for a field. By that I mean you might have a row that reads

或者,您可以扩展您的数据库以保存字段的可能值列表。我的意思是你可能有一排读着

Id=5, Text='Selected'. Value='False', Type='System.Boolean'

Id=5,文本=“选择”。值='假',类型='System.Boolean'

Then in another table

然后在另一个表

Id=5, Value=True, default=false Id=5, Value=false, default=true

Id=5,值=真,默认值=假 Id=5,值=假,默认值=真

or, for a string

或者,对于字符串

Id=5, Text='Selected'. Value='UK', Type='System.Text'

Id=5,文本=“选择”。值='英国',类型='System.Text'

Id=5, Value='UK', default=true id=5, value='USA', default=false

Id=5, Value='UK', default=true id=5, value='USA', default=false

If you do this, then you can drop the value from the first table..

如果你这样做,那么你可以从第一个表中删除值..

Not totally sure that helps?

不完全确定这有帮助吗?