0

我有一种情况,我将日期/数字等的字符串表示形式保存在数据库中,并且由于应用程序在多个国家/地区运行,有时不同国家/地区的用户使用相同的数据库,因此我将 CurrentCulture.Name 存储在数据库中记录,以便在客户端转换时,它使用正确的文化信息进行转换。例如:

DateTime dt = Convert.ToDateTime(value, new CultureInfo(value.ClientCulture));

这很好用,我的问题是,在调用 new CultureInfo(value.ClientCulture) 时,是从 .NET Framework 还是从 Windows 获取 CultureInfo?我担心如果从 Windows 调用,如果没有安装这些语言,可能会出现问题。如果从 .NET Framework 本身调用,那么我看不到任何问题。

谢谢

4

3 回答 3

3

The correct answer is: Do not store user-specific date time string in db!

Nowadays every db have datetime type, so you must convert string to datetime value using current user locale and put this value in db (instead of string).

If, in some strange case, db does not support datetime type, or maybe there is some other requirements (however I cannot invent even one), then you must convert date time into invariant datetime string, and put this string in db.

BTW, for speedup reason, you should use GetCultureInfo method instead of creating new CultureInfo instance:

DateTime dt = Convert.ToDateTime(value, CultureInfo.GetCultureInfo(value.ClientCulture));
于 2009-07-02T14:51:52.283 回答
0

“我将 CurrentCulture.Name 与记录一起存储在数据库中,以便在客户端进行转换时”

我认为您不应该将文化信息与日期一起存储

如果您想记住用户显示日期的首选方式,您可以考虑这样做,

但是在大多数情况下,日期的格式(显示方式)取决于用户的区域设置。(用户必须能够更改它们);

只要看看用户的区域设置是什么就可以了。

要回答您的问题:

从 CultureInfo.CurrentCulture 属性返回的 CultureInfo 实例基于用户在 Windows® 区域选项中选择的区域设置(如图 1 所示)。在 Windows XP 和 Windows Server™ 2003 中,这称为程序员的“用户区域设置”,以及标准和格式的“语言”。

您不必担心已安装的语言,因为始终安装用户使用的语言!(同上选定的区域设置)

请参阅http://msdn.microsoft.com/en-us/magazine/cc163824.aspx

于 2009-07-02T14:14:11.550 回答
-1

MSDN判断,我假设构造函数将抛出 ArgumentException,因为机器上不存在文化。

通过对CultureInfo 类的快速阅读,看起来 CultureInfo 对象被序列化为仅 Name 和 UseUserOverride,这意味着任何关于文化的真实信息都存储在 Windows 中,并且依赖于正确的 Name。

于 2009-07-02T14:05:03.987 回答