.net DateTime MaxValue 一旦存储在数据库中就不同
当我在数据库中存储值为 DateTime.MaxValue 的日期属性并将其检索回来时,存储的值不等于 DateTime.MaxValue。勾选属性已关闭。这是为什么呢?
使用 MS SQL,日期字段的数据类型为“datetime”
When I store date property with value DateTime.MaxValue in database and retrieve it back, the stored value does not equal to DateTime.MaxValue. The tick properties are off. Why is this?
Using MS SQL, data type for date field is 'datetime'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为 SQL 日期时间的分辨率较低。
来源
来源
Because SQL datetime has lower resolution.
source
source
这很可能是因为 .NET DateTime 不能直接转换为 SQL DateTime 类型。
我会设置该值,然后根据 < 检查刻度代码>SqlDateTime.MaxValue。
It could very well be cause a .NET DateTime doesn't directly translate to the SQL DateTime type.
I would both set the value and then check the ticks against
SqlDateTime.MaxValue
.我必须承认我对此不确定,但这可能与日期时间的准确性有关?
快速搜索一下这里有一篇关于
日期时间的精度和准确度
另外,c# 与 sql 中的日期时间精度之间可能存在不匹配?
I must admit I'm not certain on this, but it could be to do with the accuracy of datetime?
Doing a quick search here is an article on the
Precision and accuracy of DateTime
Also perhaps there is a mistmatch between the precision of datetime in c# vs sql?
您存储该值的数据字段是什么类型?
.NET
DateTime
的最大值可能超出数据库引擎中等效数据类型的容量。Of what type is the data field in which you're storing this value?
It could be that the maximum value for a .NET
DateTime
exceeds the capacity for the equivalent data type in the database engine.MS SQL Server 在最低级别对日期做了一些奇怪的事情。例如,考虑以下脚本:
这将返回:
如您所见,MS SQL 仅处理最接近的 3 毫秒。如果您在这些之间进行转换,它们将被四舍五入。当 DateTime.MaxValue 存储在 SQL 中时,可能会发生这种情况。
MS SQL Server does some odd things with dates at the lowest level. For example consider the following script:
This returns:
As you can see MS SQL is only dealing with milliseconds to the nearest 3. If you go between these they get rounded off. Is possible that this is what's happening when DateTime.MaxValue is getting stored in SQL.