Convert.ToString(DateTime的),得到英国格式而非美国 [英] Convert.ToString(DateTime) yielding UK format instead of US

查看:580
本文介绍了Convert.ToString(DateTime的),得到英国格式而非美国的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里一个C#的DateTime字符串不能因为它是被神秘格式化为英国的日期(DD / MM / YYYY)转换为SQL日期时间的问题。下面是一系列事件的:

I'm having an issue where a C# DateTime string is failing to convert to a SQL DateTime because it is mysteriously being formatted as a UK date (dd/MM/yyyy). Here is the series of events:


  1. 在美国的一个远程服务器上创建一个对象,并序列化为XML
  2. $。 b $ b
  3. XML被反序列化的CA在本地计算机上回的对象。序列化的日期如下:2011-07-13T09:56:57.0542425

  4. 应用程序试图对象保存到数据库调用前面提到的存储过程。它(不必要的),将它作为一个参数使用Convert.ToString(DateTime的)存储过程的前日期转换为字符串。

  5. 的存储过程失败,SQLException的错误转换数据类型为nvarchar以日期时间,因为它为它的DateTime类型的参数收到的字符串是在DD / MM / YYYY格式(和数据库语言是英语美国)。

  1. An object is created on a remote server in the US and serialized to xml.
  2. The xml is deserialized on a local computer in CA back to an object. The serialized date looks like this: 2011-07-13T09:56:57.0542425
  3. The application attempts to save the object to the database calling the previously mentioned stored procedure. It (needlessly) converts the date to a string before passing it as a parameter to the sproc using Convert.ToString(DateTime).
  4. The sproc fails with the SqlException "Error converting data type nvarchar to datetime" because the string it received for its DateTime typed parameter was in the dd/MM/yyyy format (and the database language is English US).

现在,代码不应该的日期时间转换为字符串只被转换回在SQL日期时间,但这个问题刚刚开始后,一切发生的(多台计算机上)的罚款超过年。于是我想到了DB的文化或操作系统必须最近刚刚更改导致他们使用不同的日期格式。令我惊讶的是,从英语(加拿大)改变操作系统(Windows 7)英语(美国),并重新启动后,问题仍然存在。为了使它更加混乱,错误并不在当本地创建同一类型的对象,而不是被反序列化,无论区域设置发生。唯一的区别是序列化的版本发生在Windows服务和本地创建的对象的版本发生在一个Windows应用程序。他们都用自己的调用Convert.ToString(DateTime的)大会的副本,但是他们正在使用该程序集的版本相同。我完全糊涂了。

Now, the code shouldn't be converting the datetime to a string only to be converted back to a datetime in SQL, but this problem just started happening (on more than one computer) after everything was fine for over a year. So I thought the culture of the DB or the OS must have just recently changed causing them to use different date formats. To my surprise, after changing the OS (Windows 7) from English(Canada) to English(US) and restarting, the problem still occurs. To make it even more confusing, the error doesn't happen when when the same type of object is created locally instead of being deserialized, regardless of the regional settings. The only difference is the serialization version happens in a Windows service and the locally created object version happens in a Windows app. They both use their own copy of the assembly that calls Convert.ToString(DateTime) but they are using the same version of that assembly. I am utterly confused.

P.S。 .NET 2.0和放大器; SQL Server 2005中

P.S. .NET 2.0 & SQL Server 2005

推荐答案

是否有可能该服务是有错误的区域设置的帐户下运行?

Is it possible that the service is running under an account which has the wrong regional settings?

如果是这样,那么也许的这些从微软的指令可能适用。

If so, then perhaps these instructions from Microsoft might apply.

这篇关于Convert.ToString(DateTime的),得到英国格式而非美国的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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