[英]System.InvalidCastException: Specified cast is not valid
I retrieve a table from the database that contains a column of type "time" in sql server, and I try to assign this to a DateTime variable but i get this error: System.InvalidCastException: Specified cast is not valid. 我从数据库中检索一个包含sql server中类型为“time”的列的表,我尝试将其分配给DateTime变量,但是我收到此错误:System.InvalidCastException:指定的强制转换无效。
here's my c# code: 这是我的c#代码:
DateTime StartTime = (DateTime)dt.Rows[i]["startTime"];
knowing that the column "startTime" is of type "time" and I can change it in the database. 知道列“startTime”是“time”类型,我可以在数据库中更改它。 any help??
任何帮助?
DateTime StartTime = Convert.ToDateTime(dt.Rows[i]["startTime"].ToString());
如果你知道它可能是null ..
DateTime StartTime = (dt.Rows[i]["startTime"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dt.Rows[i]["startTime"].ToString()));
您应该能够将其转换为TimeSpan
:
var startTime = dt.Rows<TimeSpan>("startTime");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.