【问题标题】:Converting DateTime results in IndexOutOfRangeException转换 DateTime 导致 IndexOutOfRangeException
【发布时间】:2011-09-11 15:52:37
【问题描述】:
Time =(DateTime) AllQuestionsPresented.TryParse(dr["Time"].ToString());

dr 是一个 SqlReader。

我收到了IndexOutOfRangeException,但我不知道为什么。这是 TryParse 函数:

public static DateTime? TryParse(string text)
{
    DateTime date;
    if (DateTime.TryParse(text, out date))
    {
        return date;
    }
    else
    {
        return null;
    }
}

我不明白为什么我会得到那个异常,我怎么能摆脱它?!

【问题讨论】:

  • 哪一行抛出异常?你在这行之前检查dr is not null 吗?
  • 您在一行代码中发生了多个操作。出于调试目的,请将代码分解为各个部分,以便您可以查看是哪个特定部分引发了异常。
  • 消息简单地说:“时间”

标签: c# sql indexoutofboundsexception


【解决方案1】:

这可能意味着它无法在阅读器中找到。 dr 是否有名为“时间”的列?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2016-03-11
    相关资源
    最近更新 更多