对于.NET DateTime类型,为什么是推断的数据库类型SqlDbTypes.DateTime代替SqlDbTypes.DateTime2? [英] For the .NET DateTime type, why is the inferred database type SqlDbTypes.DateTime instead of SqlDbTypes.DateTime2?

查看:470
本文介绍了对于.NET DateTime类型,为什么是推断的数据库类型SqlDbTypes.DateTime代替SqlDbTypes.DateTime2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于.NET DateTime类型,为什么是推断的数据库类型SqlDbTypes.DateTime代替SqlDbTypes.DateTime2?的(见的 http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx

通过默认为less- precise SQL DateTime类型,在.NET框架保证,在默认情况下,任何.NET日期时间值你经过与未指定的SqlDbType一个的SqlParameter对象肯定是 通过在precision减少<损坏/ EM> 。这是一个糟糕的设计决策,国际海事组织,考虑就没有更坏的后果,只需preserving的全部价值。

By defaulting to the less-precise SQL DateTime type, the .NET framework guarantees that, by default, any .NET DateTime value you pass through an SqlParameter object with an unspecified SqlDbType is sure to be corrupted via a reduction in precision. That's a bad design decision, IMO, considering there would be no worse consequences by simply preserving the full value.

例如,我不能用SqlParameterCollection.AddWithValue方法,因为传递一个DateTime值时,该值被截断为SQL日期时间值其中有一个非常有限的范围内。的结果要么是:

For example, I cannot use SqlParameterCollection.AddWithValue method, because when passing a DateTime value, the value is truncated to an SQL DateTime value which has a very limited range. The results are either:

  • 在.NET DateTime值超出一个SQL DateTime值的允许范围,就会发生错误,或者
  • 截断值不会匹配在数据库中的更precise价值,它不会正确地匹配记录更新操作,这更是雪上加霜,国际海事组织,因为它是微妙的,不会产生错误。

由于.NET日期时间最接近于SQL Server 2008的数据类型DATETIME2(7)在这两个precision和范围,为何框架的的SqlParameter值转换为一个SQL日期时间,和有没有办法来改变默认的行为,所以我仍然可以使用类型推断功能?

我能看到的唯一建议是,该功能被破坏,我应该总是明确指定数据类型,这将需要大量的code的变化。我想像会有较少的问题,如果框架简单地preserved的.NET DateTime值的原始值。如果数据库字段类型恰好是少precise SQL DateTime类型,然后传递到查询的日期/时间字符串值将简单地由数据库引擎截断。如果是出界,你会得到一个错误,符合市场预期。更重要的是,如果数据库字段类型DATETIME2,那么一切都将顺利地流动和记录将匹配正确。

The only advice I can see is that the feature is broken and I should always explicitly specify the data type, which is going to require a lot of code changes. I'd imagine there would be less problems if the framework simply preserved the original value of the .NET DateTime value. If the database field type happens to be the less precise SQL DateTime type, then the date/time string value passed to the query will simply be truncated by the database engine. If it's out of bounds, you'll get an error, as expected. More importantly, if the database field type is datetime2, then everything will flow smoothly and records will match properly.

推荐答案

DATETIME2 添加仅在SQL Server 2008中。

datetime2 was added only in SQL Server 2008.

SqlDbType.DateTime2 添加到.NET仅在.NET 3.5。

SqlDbType.DateTime2 was added to .NET only in .NET 3.5.

更改,将一直在每一个现有的.NET 2.0 / 3.0应用断裂行为的改变。

Changing that would have been a breaking behavior change on every existing .NET 2.0/3.0 application.

我不知道有任何挂钩更改类型参数推断。

I do not know of any hook to change that type parameter inference.

这篇关于对于.NET DateTime类型,为什么是推断的数据库类型SqlDbTypes.DateTime代替SqlDbTypes.DateTime2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆