使用哪种 SQL Server sql 数据类型来保留 UTC 日期时间

发布于 2024-10-08 06:54:05 字数 945 浏览 6 评论 0原文

我有一个 SQL Server DB 表,其中有一列“ReceivedDate”定义为“datetime”,其中应包含 UTC 日期...在我的 C# 代码中,我使用实体框架将表映射到一个类,该类具有相应的属性“ReceivedDate” ” 类型为 System.DateTime。

该程序将 XML 文件中的日期加载到 DB 中,并在稍后的某个时刻检查 XML 中的数据是否与 DB 中的数据相同...当 XML 中的 ReceivedDate 日期与 DB 不匹配时,检查失败...对于示例:

ReceivedDate from XML:
<ReceivedDate>2010-12-16T22:53:27.5912217Z</ReceivedDate>

ReceivedDate from DB:
2010-12-16 22:53:27.590

经过一些调试后,我注意到数据库中的日期没有将 Kind 属性设置为 Utc,并且刻度数要少得多,因此日期比较失败...

  • 如何在 SQL 中存储完整的 UTC 日期 服务器所以当实体框架 检索它,我得到 System.DateTime 与该值完全相同的值 来自 XML 文件(包括 Kind=Utc)?
  • 这只是使用的问题吗 我的列有不同的 sql 数据类型 (例如 datetime2 而不是 datetime)?

更新:

我解决这个问题的方法是:

  1. 将sql数据类型更改为“datetime2”以匹配
  2. POCO中sql数据类型和.net System.DateTime之间的精度我覆盖了Equals,在检查ReceivedDate属性时我刚刚从ReceivedDate创建了另一个DateTime变量但使用 Kind == Utc 的构造函数。

这是可行的,尽管我确实同意使用 DateTimeOffset 可能是更好的解决方案。

I have a SQL Server DB table that has a column "ReceivedDate" defined as "datetime" which should contain UTC date...In my C# code I use Entity Framework to map to table to a class, which has a corresponding property "ReceivedDate" of type System.DateTime.

The program loads the date from an XML file into DB and at some point later checks if the data in XML is same as data in DB...The check fails when dates of ReceivedDate in XML and DB don't match...For example:

ReceivedDate from XML:
<ReceivedDate>2010-12-16T22:53:27.5912217Z</ReceivedDate>

ReceivedDate from DB:
2010-12-16 22:53:27.590

After some debugging I noticed that date from DB does not have Kind property set to Utc and the number of ticks is much less and therefore comparison on dates fails...

  • How do I store full UTC date in SQL
    server so when Entity Framework
    retrieves it, I get System.DateTime
    value that is exactly same as the one
    from XML file (including Kind=Utc)?
  • Is this just a matter of using
    different sql data type for my column
    (e.g. datetime2 istead of datetime)?

Update:

The way I resolved this was to:

  1. change sql data type to "datetime2" to match precision between sql data type and .net System.DateTime
  2. in my POCO I overrode Equals and when checking ReceivedDate property I just created another DateTime variable from ReceivedDate but using constructor with Kind == Utc.

This works, although I do agree that using DateTimeOffset would probably be better solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾其所爱 2024-10-15 06:54:05

使用 datetimeoffset 存储在 SQL Server 上。

定义与具有时区意识并基于 24 小时制的一天中的时间相结合的日期。

另外,请考虑使用 DateTimeOffset 结构而不是 DateTime

Use datetimeoffset to store on SQL Server.

Defines a date that is combined with a time of a day that has time zone awareness and is based on a 24-hour clock.

Also, consider using the DateTimeOffset structure instead of DateTime in your .NET code.

ぶ宁プ宁ぶ 2024-10-15 06:54:05

Sql服务器中的datetime只有精确度为百分之三秒

.Net 中的准确率更高。因此,数据库数据是四舍五入的,并且不相同。如果你查看你的数据,错误是 0.0012217 秒。

如果可能,您可以使用 datetime2 :

0到7位,精度为100ns。默认精度为 7 位。

datetime2 使用与 .Net DateTime 相同的精度。

The datetime in Sql server only has an accuracy of one three-hundredth of a second.

The accuracy in .Net is higher. The DB data is therefore rounded, and not the same. If you look at your data, the error is 0.0012217 seconds.

If possible, you could use the datetime2:

0 to 7 digits, with an accuracy of 100ns. The default precision is 7 digits.

datetime2 uses the same accuracy as the .Net DateTime.

沫尐诺 2024-10-15 06:54:05

如果您想将数据库 UTC DateTime 转换为 C# DateTime,请执行此操作。创建 DateTime 时,请使用带有 DateTimeKind 参数的构造函数:

DateTime utcDateTime = new DateTime(((DateTime)row["myUtcDateTime"]).Ticks, DateTimeKind.Utc)

如果您正在寻找更好的方法在 SQL 中实际存储它(你已经是 2008 年了),请遵循 @Oded 的建议。

If you want to turn your database UTC DateTimes into C# DateTimes, do this. When you create the DateTime, use the constructor with the DateTimeKind parameter:

DateTime utcDateTime = new DateTime(((DateTime)row["myUtcDateTime"]).Ticks, DateTimeKind.Utc)

If you're looking for a better way to actually store it in SQL (and you're on 2008), follow @Oded's advice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文