【发布时间】:2015-08-24 23:32:57
【问题描述】:
我试图从我的 SQLite 数据库中的每一行检索 DateTime 信息,但我收到错误“字符串未被识别为有效的 DateTime”,我不太清楚为什么会发生这种情况,所以请提供帮助将不胜感激。
foreach (DataRow row in table.Rows)
{
string gameName = row["GameName"].ToString();
//DateTime releaseTime = (DateTime)row["ReleaseTime"];
DateTime releaseTimeGB = DateTime.ParseExact("dd/mm/yyyy", row["ReleaseTime"].ToString(), new CultureInfo("en-GB"));
gameInfo.Add(new GameInfo() { Name = gameName, ReleaseTime = releaseTimeGB});
}
现在,上面代码中注释掉的部分确实有效,但它以我不想要的格式检索 DateTime 信息,因此错误来自 ParseExact() 方法。在线查看后,很明显错误的确切来源是在 row["ReleaseTime"] 上调用 ToString() 方法,但我不确定如何纠正这个问题,因为这是我第一次使用 SQLite。
【问题讨论】:
-
row["ReleaseTime"]的值和类型是什么? -
@DavidG 它应该是 DateTime 因为这是我在执行 SQL 以创建行时指定的,每行的值类似于 '2015-09-01 00:00:00'跨度>