Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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# System.InvalidCastException:指定的强制转换无效_C#_Sql Server_Datetime - Fatal编程技术网

C# System.InvalidCastException:指定的强制转换无效

C# System.InvalidCastException:指定的强制转换无效,c#,sql-server,datetime,C#,Sql Server,Datetime,我从sql server中包含“time”类型列的数据库中检索到一个表,并尝试将其分配给DateTime变量,但出现以下错误: System.InvalidCastException:指定的强制转换无效 这是我的c代码: 知道“startTime”列的类型是“time”,我可以在数据库中更改它。 任何帮助???您应该能够将其转换为TimeSpan: var startTime = dt.Rows<TimeSpan>("startTime"); var startTime=dt.Ro

我从sql server中包含“time”类型列的数据库中检索到一个表,并尝试将其分配给DateTime变量,但出现以下错误: System.InvalidCastException:指定的强制转换无效

这是我的c代码:

知道“startTime”列的类型是“time”,我可以在数据库中更改它。
任何帮助???

您应该能够将其转换为
TimeSpan

var startTime = dt.Rows<TimeSpan>("startTime");
var startTime=dt.Rows(“startTime”);
如果你知道它可能是空的

DateTime StartTime = (dt.Rows[i]["startTime"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dt.Rows[i]["startTime"].ToString()));

退房看起来你需要使用一个时间跨度。德文是对的。在.NET中,SQL Server 2008时间类型映射到TimeSpan对象。
DateTime StartTime = Convert.ToDateTime(dt.Rows[i]["startTime"].ToString());
DateTime StartTime = (dt.Rows[i]["startTime"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dt.Rows[i]["startTime"].ToString()));