Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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# IsDBNull抛出一个异常_C# - Fatal编程技术网

C# IsDBNull抛出一个异常

C# IsDBNull抛出一个异常,c#,C#,我正在阅读MySql数据库,其中一列是DateTime类型的。其中一些日期为空。我似乎不知道如何检查值是否为null。 我原以为IsDbNull可以解决这个问题,但这根本没什么区别 if (!reader.IsDBNull(4)) { service.ServiceDate = reader.GetDateTime("serviceDate"); } 这是我得到的一个例外: 其他信息:无法将MySQL日期/时间值转换为 系统日期时间 我也试过了 if (System.DBNull.Val

我正在阅读MySql数据库,其中一列是DateTime类型的。其中一些日期为空。我似乎不知道如何检查值是否为null。 我原以为IsDbNull可以解决这个问题,但这根本没什么区别

if (!reader.IsDBNull(4))
{
   service.ServiceDate = reader.GetDateTime("serviceDate");
}
这是我得到的一个例外:

其他信息:无法将MySQL日期/时间值转换为 系统日期时间

我也试过了

if (System.DBNull.Value == reader.IsDBNull(4))
以及

reader.IsDBNull(4).ToString()


如何在不使用try-catch的情况下检查它?

该错误似乎与IsDBNull无关-它特别提到了日期时间转换。您是否尝试过将列索引传递给GetDateTime,就像使用IsDbNull一样

另一种可能性:Convert Zero Datetime=true

此外,在阅读了有关错误的其他信息后,听起来您可能需要将Convert Zero Datetime=true添加到连接字符串中,这样可以将Datetime列中的0000-00-00:00:00值转换为Datetime.MinValue


在这里查看一些结果:

读卡器的类型是什么?您正在读取的日期时间值是什么?它可能超出System.DateTime.IsDBNull的范围,但不会引发该错误。但是reader.GetDateTimeserviceDate应该会导致编译器错误,因为GetDateTime只接受int作为列索引的参数。显示真实代码。可能相关:Blorgbeard,GetDateTime有一个字符串重载,引发错误的是IsDBNull。我试图通过即时窗口查看结果,从而确保了这一点。
Convert.ToBoolean(reader.IsDBNull(4))
if (!reader.IsDBNull(4))
{
   service.ServiceDate = reader.GetDateTime(4);
}