日期格式和区域设置

发布于 2024-08-23 12:13:29 字数 442 浏览 7 评论 0原文

我正在使用 MS SQL 2000、VS2008、MVC 和 C#。

我正在尝试使用存储过程插入和更新一些数据。 有些列的类型为日期时间。 服务器和客户端上的区域设置均设置为荷兰语(比利时) 这意味着默认日期格式为 dd/mm/yyyy。

当我尝试插入或更新日期时,例如。 2009 年 3 月 28 日,我收到以下错误:

插入: 将数据类型 nvarchar 转换为 datetime 时出错

更新: 将 char 数据类型转换为 datetime 数据类型导致日期时间值超出范围

当我尝试使用 01/03/2009 这样的日期时,没有收到任何错误,但日期保存为 03/01/2009 ,这是美国日期格式。 这是区域设置问题的典型行为。但两者都设置为荷兰语(比利时)。

为什么它以美国格式保存日期?
我在这里缺少什么?

谢谢!
斯泰因

I'm using MS SQL 2000, VS2008, MVC and C#.

I'm trying to insert and update some data using stored procedures.
Some columns are of type datetime.
Regional settings on both server and client are set to Dutch (Belgium)
This means the default date format is dd/mm/yyyy.

When i try to insert or update with a date of eg. 28/03/2009, I get following errors:

Insert:
Error converting data type nvarchar to datetime

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

When I try with a date like 01/03/2009, I get no errors but the date is saved as 03/01/2009, which is the US date format.
This is typical behaviour for problems with regional settings. But both are set to Dutch (Belgium).

Why does it save dates in the US format?
What am i missing here?

Thanks!
Stijn

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

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

发布评论

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

评论(4

吐个泡泡 2024-08-30 12:13:29

您应该使用 DateTime 对象而不是字符串将数据插入数据库。您的客户端代码应使用客户端的区域设置将客户端的日期条目转换为 DateTime 对象,然后应将 DateTime 结构添加到最终发送到数据库的参数中。

You should be inserting data into the database using a DateTime object, not a string. Your client-side code should convert the client's date entry to a DateTime object using the client's regional settings, then the DateTime struct should be added to the parameter that is ultimately sent into the database.

Bonjour°[大白 2024-08-30 12:13:29

SQL 实例有自己的区域设置,默认为“us_english”。

现在,如果您使用 varchar 而不是本机日期时间来存储数据值,通常会发生这种情况。如果您的代码/表使用日期时间列并且您将参数定义为日期时间,那么您不会收到错误。

The SQL Instance has it's own locale setting, by default "us_english"

Now, this usually happens if you pushing using varchar rather than native datetime to store data values. If your code/tables use datetime columns and you define parameters as datetime then you won't get errors.

夏九 2024-08-30 12:13:29

我也遇到了这个问题,它与 SQL Server 的日期格式有关,

我通过格式化要插入的日期字符串来解决它,

DateTime.Now.ToString("MM/dd/yyyy HH:mm")

希望能有所帮助

i had this problem too, its something to do with the date format for you SQL server,

i solved it by formatting the date string to be inserted like so

DateTime.Now.ToString("MM/dd/yyyy HH:mm")

hope that helps

秋意浓 2024-08-30 12:13:29

以上所有建议都是正确的,但我发现如果您将日期时间添加为字符串/varchar,最安全的方法是采用“YYYY-MM-DD”格式,

例如。

  Update MyTable
  Set MyDate = '2010-03-01'

All above suggestions are correct but I find if you are adding a datetime as a string/varchar the safest way is in the format 'YYYY-MM-DD'

So eg.

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