如何防止超出日期时间值错误? [英] How to prevent out-of-range datetime value error?

查看:183
本文介绍了如何防止超出日期时间值错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个临时插入语句从c#应用程序传递到sql server 2000/2005。有时,如果机器(其中安装了sql server),datetime格式与我传递的格式不同,则会抛出错误。



:在插入语句中,我正在通过2010-03-10 00:00:00-05:00,但机器区域日期设置不同。我收到此错误: -


将char数据类型转换为
a datetime数据类型导致
超出范围的日期时间值。


我可以在C#中的Insert语句中传递一些通用日期格式,与任何机器完美配合区域日期时间设置?

解决方案

yyyymmdd是最安全的,基本的ISO-8601格式。 yyyy-mm-dd有以下链接的问题



随着时间的推移:yyyymmdd hh:mm:ss



SQL Server可以在处理数据时间时稍微 。它主要在SQL Server 2008中使用新的日期和时间格式进行修复。 Tibor Karaszi 最终文章



编辑:另一个 Tony Rogerson的文章对于不信者


I am passing an ad-hoc Insert statement from c# application to the sql server 2000/2005. Sometimes, if machine (where sql server is installed) datetime format is different than what I am passing in, it is throwing an error.

e.g. : In Insert statement I am passing '2010-03-10 00:00:00-05:00' this but machine regional date setting is different. I am getting this error:-

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

Can I pass some generic date format in Insert statement from c# that works perfectly with any machine Regional Date Time settings.?

解决方案

"yyyymmdd" is safest, the basic ISO-8601 format. "yyyy-mm-dd" has issues as the link below mentions

With times: "yyyymmdd hh:mm:ss"

SQL Server can be slightly odd when dealing with datetimes. It's mostly fixed in SQL Server 2008 with the new date and time formats. The definitive article by Tibor Karaszi

Edit: And another article by Tony Rogerson for the unbelievers

这篇关于如何防止超出日期时间值错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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