【问题标题】:Read out Datetime from SQLite with C#使用 C# 从 SQLite 中读取日期时间
【发布时间】:2018-07-30 18:02:51
【问题描述】:

我创建了一个带有 sqllite DB 的程序,我可以在其中保存数据(id/date/name)。

数据库:

1 - 15.09.2016 00:00:00 - Test
2 - 28.06.2018 00:00:00 - Test2

类型:数字

我尝试使用 SQLite 阅读器阅读此内容。

代码:

String sql = $"SELECT * FROM test";
SQLiteCommand command = new SQLiteCommand(sql, dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
  Console.WriteLine($"Date: {reader["date"]}");
  ...
}

输出:

15,09
28,06

如果我将其转换为日期时间,我只会得到 2018 年:

15.09.2018
28.06.2018

如何以正确的格式读出日期时间和年份?

【问题讨论】:

  • 类型很重要,您可以使用 SQLiteDataAdapter。 devart.com/dotconnect/sqlite/docs/…
  • 尝试以下操作:reader["date"].ToString("dd.MM.yyyy")
  • @jdweng,我认为 SQLite 没有 DateTime 数据类型。我们必须解析 ISO8601 字符串。
  • SQLListe 可能没有日期时间,但驱动程序可能正在转换为日期时间。它就像使用常规格式的 Excel,它试图自动识别数据类型。在 Excel 中,执行转换的是 Jet Engine 或 ACE 驱动程序。 SQLite 驱动程序也可能是真正引起问题的 Jet 引擎。
  • Arg 我无法测试它。但是阅读器上有一个GetDateTime,所以应该/可以工作。

标签: c# sqlite


【解决方案1】:

https://www.sqlite.org/datatype3.html

SQLite 没有 DateTime 数据类型。您应该解析字符串 ISO8601。

DateTime myDateTime = DateTime.ParseExact(myString, myFormat, CultureInfo.InvariantCulture);

SqliteDataReader 有一个GetDateTime 方法。

reader.GetDateTime(1).ToString("dd.MM.yyyy")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 2010-12-22
    • 2013-10-16
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多