使用哪种 SQL Server sql 数据类型来保留 UTC 日期时间
我有一个 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)?
更新:
我解决这个问题的方法是:
- 将sql数据类型更改为“datetime2”以匹配
- 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:
- change sql data type to "datetime2" to match precision between sql data type and .net System.DateTime
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
datetimeoffset
存储在 SQL Server 上。另外,请考虑使用
DateTimeOffset
结构而不是DateTime
。Use
datetimeoffset
to store on SQL Server.Also, consider using the
DateTimeOffset
structure instead ofDateTime
in your .NET code.Sql服务器中的
datetime
只有精确度为百分之三秒。.Net 中的准确率更高。因此,数据库数据是四舍五入的,并且不相同。如果你查看你的数据,错误是 0.0012217 秒。
如果可能,您可以使用
datetime2
:datetime2
使用与 .NetDateTime
相同的精度。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
:datetime2
uses the same accuracy as the .NetDateTime
.如果您想将数据库 UTC
DateTime
转换为 C#DateTime
,请执行此操作。创建DateTime
时,请使用带有DateTimeKind
参数的构造函数:如果您正在寻找更好的方法在 SQL 中实际存储它(你已经是 2008 年了),请遵循 @Oded 的建议。
If you want to turn your database UTC
DateTime
s into C#DateTime
s, do this. When you create theDateTime
, use the constructor with theDateTimeKind
parameter:If you're looking for a better way to actually store it in SQL (and you're on 2008), follow @Oded's advice.