简体   繁体   English

无法将DBNull.Value强制转换为“System.DateTime”。 请使用可空类型

[英]Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type

This is my code 这是我的代码

var finalResults = (from r in results.AsEnumerable()
                    where r.Field<DateTime>("PrintTime") is DBNull

where PrintTime is a column in my Sql Server 2008 r2 database, its type is datetime and it is nullable 其中PrintTime是我的Sql Server 2008 r2数据库中的一列,其类型是datetime ,可以为

I got this exception: 我有这个例外:

Cannot cast DBNull.Value to type 'System.DateTime'. 无法将DBNull.Value强制转换为“System.DateTime”。 Please use a nullable type. 请使用可空类型。

Could you help please? 你能帮帮忙吗?

DataRow.Field supports nullable types, so use DateTime? DataRow.Field支持可空类型,所以使用DateTime? instead of DateTime : 而不是DateTime

var finalResults = from r in results.AsEnumerable()
                   let printTime = r.Field<DateTime?>("PrintTime")
                   where !printTime.HasValue
                   select r;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将DBNull.Value强制转换为'System.DateTime',请使用可空类型 - Cannot cast DBNull.Value to type 'System.DateTime', Please use nullable types 无法将DBNull.Value强制转换为“System.DateTime”。 请使用可空类型。 关于使用Linq查询 - Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type. on using Linq Query 无法将DBNull.Value强制转换为System.DateTime类型 - Cannot cast DBNull.Value to type System.DateTime 无法将DBNull.Value强制转换为类型“ System.Double”。 请使用可为空的类型 - Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type ASP.NET站点崩溃无法强制转换DBNull.Value以键入&#39;System.Guid&#39; - ASP.NET Site crashing on Cannot cast DBNull.Value to type 'System.Guid' null 值不能分配给 System.DateTime 类型的成员,该类型是不可为空的值类型 - The null value cannot be assigned to a member with type System.DateTime which is a non-nullable value type 无法将“System.DBNull”类型的 object 转换为“System.DateTime”类型 - Unable to cast object of type 'System.DBNull' to type 'System.DateTime' Object 类型 'System.DBNull' 无法转换为类型 'System.DateTime' - Object of type 'System.DBNull' cannot be converted to type 'System.DateTime' SetField 无法将 DBNull.Value 转换为 System.Int32 - SetField cannot cast DBNull.Value to System.Int32 EF AsNoTracking导致错误。 类型&#39;System.DateTime&#39;的表达式不能用于赋值类型&#39;System.Nullable`1 [System.DateTime]&#39; - EF AsNoTracking Causes error. Expression of type 'System.DateTime' cannot be used for assignment to type 'System.Nullable`1[System.DateTime]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM