Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未正确设置C#数据表列类型_C#_Types_Datatable - Fatal编程技术网

未正确设置C#数据表列类型

未正确设置C#数据表列类型,c#,types,datatable,C#,Types,Datatable,数据表中的类型有问题。在下面的代码中,我有一个数据表,并为每个列指定类型: logItems = new DataTable(); logItems.Columns.Add(new DataColumn("Machine_Name",typeof(string))); logItems.Columns.Add(new DataColumn("Time_Stamp", typeof(DateTime))); logItems.Colu

数据表中的类型有问题。在下面的代码中,我有一个数据表,并为每个列指定类型:

        logItems = new DataTable();
        logItems.Columns.Add(new DataColumn("Machine_Name",typeof(string)));
        logItems.Columns.Add(new DataColumn("Time_Stamp", typeof(DateTime)));
        logItems.Columns.Add(new DataColumn("Message_Text", typeof(string)));
        logItems.Columns.Add(new DataColumn("Message_Type",typeof(MsgType)));
后来,当我尝试使用数据表时,就好像没有设置表的类型一样。例如,我用一些数据填充表

            DataRow dataRow = logItems.NewRow();
            dataRow["Machine_Name"] = machineName;
            dataRow["Time_Stamp"] = GetDateTime(lineItem);
            dataRow["Message_Text"] = GetMsgText(lineItem);
            dataRow["Message_Type"] = GetMsgType(dataRow["Message_Text"]);
            logItems.Rows.Add(dataRow);
GetMsgType
函数的签名是
MsgType GetMsgType(string messageText)

我在使用
dataRow[“Message_Type”]=GetMsgType(dataRow[“Message_Text”])
行时遇到问题:如果后面没有
ToString()
,它就无法工作,我很难理解原因。当我使用
NewRow()
方法创建新的
DataRow
时,它会创建与
logItems
相同方案的数据行。因此,列
“Message_Text”
的类型为
string
,因此,当我在
GetMsgType
函数中使用它时,我应该能够这样使用它,但它没有按预期工作,因为我收到了“无法从对象转换为字符串”错误

类似地,我在比较
“Message\u Type”
列时遇到问题。该列被声明为type
enum MsgType{}
(见上文),就我所知,将其作为一种合法的方式使用是合法的:

        DataRow[] errorSubset = logItems.Select(query);
        foreach(DataRow lineACDC in errorSubset)
        {
            if (lineACDC["Message_Type"] == MsgType.Error) 
            {

非常感谢您的见解。

什么是MsgType?它是枚举吗?使用datarow[“column name”]从datatable访问的所有内容都是对象。检索时需要将其强制转换为目标类型。@jdweng:是的,MsgType是一个枚举。@ChetanRanpariya谢谢。。这很有趣:那么为什么在创建数据表时需要指定一个类型,而在检索数据表时,它会显示为一个对象。DataRow表示存储在列中的项的集合。现在,您不能有一个允许您存储不同类型数据的集合,当您检索时,您将获得相同类型的数据。在C#中没有这样的集合。如果要在集合中存储具有不同数据类型的数据,则集合需要表示一些基本类型,
object
是.NET中所有类型的基本类型。这就是为什么datarow[ColumnName]将数据作为对象提供给您,并且您需要将其强制转换为特定的目标类型。