c# - 在 C# 和 sqlite 中,日期时间值被错误插入或错误查询

标签 c# sqlite datetime insert

我在 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/

相关文章:

c# - ASP .NET MVC 表单字段验证(无模型)

c# - 如何在 Blazor 中使用 TagHelpers?

android - 我应该使用哪种服务器型号? SQlite 还是 MySql?

javascript - 使用 moment.js 从两个变量设置日期和时间

javascript - Mongoose 从 html datepicker 中查找 2 个日期之间的结果

c# - 使用 LINQ 从 ArrayList 中删除项目集

C# 与基于列表的属性相等

c++ - 错误 C2440 : 'initializing' : cannot convert from

android - sqlite 使用表中的额外字段升级数据库并保留旧数据

datetime - 从<开始日期>/<持续时间> XBRL-JSON格式提取elasticsearch日期