我在 C# 中将日期时间值插入到 sqlite 数据库时遇到问题。后来我找到了一种方法来做到这一点。现在我可以通过像这样格式化它们来插入日期时间值
data.Add("sayacTarihZamani", string.Format("{0:u}", MydateTimeData));
我可以看到使用 sqlite 管理工具将数据正确插入到数据库表中。 当我查询这些数据时,出现的值是错误的。该值比正常值晚 3 小时。
例如,在管理工具中,我看到 30.03.2011 16:00:00
值,但在 C# 网格中,该值为 30.03.2011 19:00:00
..
有一个异常(exception)。我使用管理工具手动输入一行。它的日期值为
30.03.2011 11:30:00
在数据网格中它仍然是 30.03.2011 11:30:00
(true)
如何使用 sqlite 和 C# 正确插入和查询日期时间数据。
感谢您的宝贵时间,
费尔达
最佳答案
当您使用 u
说明符时,您的 DateTime 会自动转换为通用时间。 From the docs (强调我的):
The "U" standard format specifier represents a custom date and time format string that is defined by a specified culture's DateTimeFormatInfo.FullDateTimePattern property. The pattern is the same as the "F" pattern. However, the DateTime value is automatically converted to UTC before it is formatted.
如果您想存储本地时间,请尝试使用 F
模式。
关于c# - 在 C# 和 sqlite 中,日期时间值被错误插入或错误查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9163041/