简体   繁体   English

在C#中格式化SQL友好日期/时间

[英]Formatting SQL friendly Date/Times in C#

I am building some test scenarios for my application. 我正在为我的应用程序构建一些测试场景。 In order to do this, I have some code that is generating some SQL to help me test properly. 为了做到这一点,我有一些代码生成一些SQL来帮助我正确测试。 A sample of the SQL that is generated is shown here: 此处显示了生成的SQL示例:

INSERT INTO MyTable
(
 [ID],
 [Name],
 [CheckInDate],
 [CheckOutDate]
)
VALUES
(
  1,
  'A title',
  'Sat, 17 Dec 2011 14:33:12 GMT',
  'Sat, 17 Dec 2011 15:13:12 GMT'
)

When I attempt to execute this SQL, I receive the following: Conversion failed when converting date and/or time from character string. 当我尝试执行此SQL时,收到以下内容: 从字符串转换日期和/或时间时转换失败。

How can i format the date/time strings so that SQL Server 2008 will accept them? 如何格式化日期/时间字符串以便SQL Server 2008接受它们? I really need my C# code to generate the SQL (including the date/time items) to create my tests properly. 我真的需要我的C#代码来生成SQL(包括日期/时间项)来正确创建我的测试。

Thank you! 谢谢!

The way to solve this is to use the ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings. 解决此问题的方法是使用SQL Server支持的ISO-8601日期格式 - 此格式始终有效 - 无论您的SQL Server语言和日期格式设置如何。

The ISO-8601 format is supported by SQL Server comes in two flavors: SQL Server支持ISO-8601格式有两种形式:

  • YYYYMMDD for just dates (no time portion) - note here: no dashes! YYYYMMDD仅适用于日期(无时间部分) - 请注意: 没有破折号! , that's very important! ,这非常重要! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations! YYYY-MM-DD 不是独立的在SQL Server中的日期格式设置,并不会在所有情况下工作!

or: 要么:

  • YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes. YYYY-MM-DDTHH:MM:SS表示日期和时间 - 请注意:此格式破折号。

This is valid for SQL Server 2000 and newer. 这适用于SQL Server 2000及更高版本。

If you use SQL Server 2008 and the DATE datatype (only DATE - not DATETIME !), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server. 如果您使用SQL Server 2008和DATE数据类型(仅DATE - 而不是 DATETIME !),那么您确实也可以使用YYYY-MM-DD格式,这也适用于SQL Server中的任何设置。

Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. 不要问我为什么整个主题如此棘手而且有些混乱 - 这就是它的方式。 But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server. 但是使用YYYYMMDD格式,您可以适用于任何版本的SQL Server以及SQL Server中的任何语言和日期格式设置。

You can use SqlParameters . 您可以使用SqlParameters

So your command would be: 所以你的命令是:

INSERT INTO MyTable
(
 [ID],
 [Name],
 [CheckInDate],
 [CheckOutDate]
)
VALUES
(
  1,
  'A title',
  @CheckInDate,
  @CheckOutDate
)

And you'd insert the dates like so: 你会像这样插入日期:

SqlParameter checkin = new SqlParameter("@CheckInDate", SqlDbType.DateTime);
SqlParameter checkout = new SqlParameter("@CheckOutDate", SqlDbType.DateTime);

checkin.Value = DateTime.Today; // Format these to the desired dates
checkout.Value = DateTime.Today;

command.Parameters.Add(checkin);
command.Parameters.Add(checkout);

更好的方法是根本不格式化日期,参数化insert语句,并将日期作为命令参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM