C# ODBCDataReader 读取空数据和空日期值

发布于 2024-09-09 05:19:14 字数 753 浏览 3 评论 0原文

我正在通过 ODBC 连接从 DB2 数据库读取记录。数据被填充到 ODBCDataReader 中。当我执行 foreach 循环时,我在尝试解析多个不同的 DateTime 字段时遇到了问题。

有些字段为空,有些字段的日期时间值为空 (9999-12-31 24:00:00.000000),有些字段的日期时间值为有效 (2010-07-09 20:43:32.037234)。

我尝试过执行类似的操作来捕获空日期时间错误:

if (!dr[dbFieldName].Equals(DBNull.Value))
{
  if (dr.GetDate(dr.GetOrdinal(dbFieldName)).Equals(DateTime.Parse("9999-12-31 24:00:00.000000")))
  {
    fieldValues[tag] = "";
  }
  else
  {
    strValue = dr.GetDate(dr.GetOrdinal(dbFieldName)).ToString("s");
    fieldValues[tag] = strValue.Trim();
  }
}

GetType().Name != "DBNull" 似乎适用于捕获空值。但是,下一个 if 语句会引发 ArgumentOutOfRangeException 错误。这似乎发生在具有 9999-12-31 24:00:00.000000 值的字段上。

有没有办法正确解析这个?似乎我尝试评估这些空日期时间字段的任何方式都会引发错误。

I'm reading a record from a DB2 database via ODBC connection. The data is populated into an ODBCDataReader. As I'm going through my foreach loop I'm running into problems trying to parse the multiple different DateTime fields.

Some of the fields are null, some have null date time value (9999-12-31 24:00:00.000000) and some have valid date time values (2010-07-09 20:43:32.037234).

I've tried doing something like this to catch null date time errors:

if (!dr[dbFieldName].Equals(DBNull.Value))
{
  if (dr.GetDate(dr.GetOrdinal(dbFieldName)).Equals(DateTime.Parse("9999-12-31 24:00:00.000000")))
  {
    fieldValues[tag] = "";
  }
  else
  {
    strValue = dr.GetDate(dr.GetOrdinal(dbFieldName)).ToString("s");
    fieldValues[tag] = strValue.Trim();
  }
}

The GetType().Name != "DBNull" seems to work for catching null values. However the next if statement throws an ArgumentOutOfRangeException error. This appears to happen on fields with the 9999-12-31 24:00:00.000000 values.

Is there a way to properly parse this? It seems like any way I try to evaluate these null date time fields a error is thrown.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆依然 2024-09-16 05:19:14

除了杰罗德的评论。

DateTime.Parse("9999-12-31 23:59:00.000000") is valid
DateTime.Parse("9999-12-31 24:00:00.000000") is invalid

更新...

问题是 24:00 不被视为 24 小时制中的有效时间(无论日期如何)。您可以通过

    DateTime.ParseExact("9999-12-31 24:00:00.000000", "yyyy-MM-dd 24:mm:ss.ffffff",
System.Globalization.CultureInfo.InvariantCulture); 

在 DateTime.TryParse 方法之后使用来解决此问题。如果 TryParse 成功,则您有一个有效的日期。如果返回 false,则执行上面的语句。

In addition to Jerod's comment.

DateTime.Parse("9999-12-31 23:59:00.000000") is valid
DateTime.Parse("9999-12-31 24:00:00.000000") is invalid

update....

The problem is 24:00 is not considered a valid time in the 24 hour clock (regardless of date). You may be able to work around that by using

    DateTime.ParseExact("9999-12-31 24:00:00.000000", "yyyy-MM-dd 24:mm:ss.ffffff",
System.Globalization.CultureInfo.InvariantCulture); 

after a DateTime.TryParse method. If TryParse succeeds, you have a valid date. If it returns false, the above statemen tis executed.

千仐 2024-09-16 05:19:14

两个建议:

1) 使用 DBNull.Value 对象检查 null。
2) 使用 DateTime.MaxValue 检查 12/31/999 24:00:00。

希望这将帮助您更接近找到问题。

Two suggestions:

1) Use the DBNull.Value object to check for null.
2) Use DateTime.MaxValue to check 12/31/999 24:00:00.

Hopefully that will help you get closer to finding the problem.

铁轨上的流浪者 2024-09-16 05:19:14

将 IF/ELSE 包装在 TRY/CATCH 中。这不被认为是好的形式,但捕获异常是保证工作的。

Wrap the IF/ELSE in a TRY/CATCH. It's not considered good form, but catching the exception is gauranteed to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文