SQL 紧凑日期时间怪异

发布于 2024-09-15 13:02:50 字数 994 浏览 4 评论 0原文

我遇到了一个关于 Microsoft SQL Compact Edition 的奇怪问题。在 Windows.Forms 应用程序中,我尝试修改由同一(.Net 2.0)应用程序创建的数据库。数据库稍后会发送到 Windows Mobile Phone,但这并不重要。重要的是,在我的计算机的正常区域设置(英语(美国))上,在数据库中插入日期时间值不会出现任何问题。但是,当我将语言环境切换为荷兰语(荷兰)时,出现以下错误:

“日期格式的一部分出现错误。[表达式(如果已知)= ]”

我追踪到 DateTime 的外观在荷兰。但是,我不使用 DateTime.ToString()。我将其纯粹作为“column =” + DateTime 添加到 SQL 插入/更新语句中。这在“en-US”中完美运行,但当我切换到荷兰语时,它就崩溃了。

我解决这个问题的方法是为 DateTime 数据类型创建一个扩展方法,如下所示:

/// <summary>
/// Transforms a DateTime from various cultures into what SQL compact expects to get.
/// </summary>
/// <param name="original">DateTime to process.</param>
/// <returns>A SQL-friendly string.</returns>
public static string _ToSQLDateTimeString (this DateTime? original)
{ //No provided Date? Bye.
  if (original == null) return null;
  IFormatProvider usDate = new CultureInfo("en-US");
  return ((DateTime)original).ToString(usDate);
}

但我希望有人能够确认/改进我的解决方案。也许我错过了什么?

I'm having a weird issue concerning Microsoft SQL Compact Edition. In a Windows.Forms application, I try to modify a database which was created by the same (.Net 2.0) application. The database gets sent to a Windows Mobile Phone later, but this is not important. What's important is that on the normal regional settings of my computer, which is English (USA), inserting DateTime values in the database happens without any problems. However, when I switch the locale to Dutch (Netherlands), I get the following error:

"There was an error in a part of the date format. [ Expression (if known) = ]"

I tracked it down to the way DateTime looks in NL. However, I do not use DateTime.ToString(). I add it to the SQL insert/update statements purely as "column = " + DateTime. This works perfectly in "en-US" but when I switch to Dutch, it blows up.

The way I fixed this is by creating an extension method for the DateTime datatype like so:

/// <summary>
/// Transforms a DateTime from various cultures into what SQL compact expects to get.
/// </summary>
/// <param name="original">DateTime to process.</param>
/// <returns>A SQL-friendly string.</returns>
public static string _ToSQLDateTimeString (this DateTime? original)
{ //No provided Date? Bye.
  if (original == null) return null;
  IFormatProvider usDate = new CultureInfo("en-US");
  return ((DateTime)original).ToString(usDate);
}

But I would like somebody to maybe confirm / improve my solution. Maybe I missed something?

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

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

发布评论

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

评论(2

浊酒尽余欢 2024-09-22 13:02:50

但是 "column = " + DateTime 隐式调用 DateTime 上的 ToString()

您应该使用 System.Data.SqlClient.SqlCommand 并使用 command.Parameters.Add(...) 添加日期(以及所有其他参数)。

这将解决日期、浮点数等所有本地化问题。并保护您的应用程序免受 SQL 注入攻击。

But "column = " + DateTime implicitly calls ToString() on the DateTime.

You should use System.Data.SqlClient.SqlCommand and add the dates (and all other parameters) with command.Parameters.Add(...).

This will fix all localization issues with dates, floats, etc. And protect your application from SQL injection attacks.

他是夢罘是命 2024-09-22 13:02:50

如果要将 DATETIME 作为字符串推送到数据库中,最好使用 2010-08-25T07:26:05 格式 - 但是,更好的方法是创建一个具有正确类型参数的命令对象,然后设置将 DATETIME 参数传递给 DateTime 对象,而不是对其进行字符串化。

SQL Server 不会转换任意日期字符串格式 - 它将使用其运行所在区域设置指定的格式(默认为美国)。

If you want to push the DATETIME into the database a string, you're best of using the 2010-08-25T07:26:05 format - however, better would be to create a command object with parameters of the right types and then set the DATETIME parameter to the DateTime object rather than stringifying it.

SQL Server won't convert abritrary date string formats - it'll use the format dictated by whichever locale it's running under (default is US).

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