简体   繁体   English

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

[英]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.

相关问题 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar System.InvalidCastException:&#39;指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' System.InvalidCastException:指定的强制转换无效(linq查询) - System.InvalidCastException: Specified cast is not valid (linq query) System.InvalidCastException:指定的强制转换无效。 -DynamoDB查询 - System.InvalidCastException: Specified cast is not valid. - DynamoDB Query System.InvalidCastException:“指定的演员表无效。” C# MYSQL - System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL 错误:GetCharacters System.InvalidCastException:指定的强制类型转换无效的服务器 - Error: GetCharacters System.InvalidCastException: Specified cast is not valid server System.InvalidCastException:指定的强制转换无效。错误 - System.InvalidCastException: Specified cast is not valid. Error 为什么我收到“System.InvalidCastException: Specified cast is not valid”? - Why am I getting "System.InvalidCastException: Specified cast is not valid"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM