简体   繁体   English

将DateTime解析为SQL Server的最佳方法

[英]Best way to parse DateTime to SQL server

I was wondering what is the best way to parse a DateTime object to your SQL server. 我想知道什么是将DateTime对象解析到SQL服务器的最佳方法。

Where you are generating the SQL in code. 您在代码中生成SQL的位置。

I have always used something like DateTime.Now.TolongDateString() and had good results, apart from today where i got a error, and it made me think. 我总是使用像DateTime.Now.TolongDateString()这样的东西并且取得了很好的效果,除了今天我得到了一个错误,它让我思考。

System.Data.SqlClient.SqlException: Conversion failed when converting datetime from character string

So what is everyone thoughts and recomendations for a way that will work acrss all SQL server no matter what there setup.. 那么每个人的想法和推荐是什么方式,无论有什么设置,都可以运行所有SQL服务器。

Maybe something like DateTime.Now.ToString("yyyy/MM/dd") 也许像DateTime.Now.ToString("yyyy/MM/dd")

there are only 2 safe formats 只有2种安全格式

ISO and ISO8601 ISO和ISO8601

ISO = yymmdd ISO = yymmdd

ISO8601 = yyyy-mm-dd Thh:mm:ss:mmm(no spaces) (notice the T) ISO8601 = yyyy-mm-dd Thh:mm:ss:mmm(无空格)(注意T)

See also here: Setting a standard DateFormat for SQL Server 另请参见此处: 为SQL Server设置标准DateFormat

I found this specific format was needed: 我发现需要这种特定的格式:

DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fff")

Note casing and absence of any spaces. 注意外壳和没有任何空格。 This is the universal way according to Robyn Page . 根据Robyn Page的说法,这是一种普遍的方法。

Why not parameterise the query and pass the DateTime value as a SQL DateTime input param? 为什么不参数化查询并将DateTime值作为SQL DateTime输入参数传递?

eg INSERT INTO SomeTable (Blah, MyDateTime) VALUES (1, @MyDateTime) 例如INSERT INTO SomeTable (Blah, MyDateTime) VALUES (1, @MyDateTime)

Then you can be really sure. 那你就可以肯定了。 Even if you're generating the SQL you should be able to handle this specifically? 即使您正在生成SQL,您应该能够专门处理这个问题吗?

You should really use parametrized queries for that and pass your DateTime object as a SQL DateTime parameter. 您应该使用参数化查询,并将DateTime对象作为SQL DateTime参数传递。

If you parse the DateTime to String you will have to deal with the localisation settings both on your application server as on the database server. 如果将DateTime解析为String ,则必须在应用程序服务器上处理数据库服务器上的本地化设置。 That can lead to some nasty surprises like treating the Date as it was in US format on one side and eg UK on another. 这可能会导致一些令人讨厌的惊喜,例如将日期视为美国格式,另一方面是英国。 The string 9/12/2000 can be either September the 12th or the 9th of December. 字符串9/12/2000可以是9月12日或12月9日。 So keep the data in the DateTime object/type when exchanging between application and database. 因此,在应用程序和数据库之间进行交换时,请将数据保留在DateTime对象/类型中。

The only time you would parse DateTime to String would be when displaying data (GUI). 只有在将DateTime解析为String时才会显示数据(GUI)。 But then you should make sure to use the proper localization setting when parsing to display it in the format the user is expecting. 但是,在解析时,应确保使用正确的本地化设置,以用户期望的格式显示它。

The same principle applies to other data types like float for example. 同样的原则适用于其他数据类型,例如float The string representation of this varies depending on the locale and I suppose you do not parse float to String when passing it to the database so why do it with the DateTime ? 这个字符串的表示形式取决于语言环境,我想你在将它传递给数据库时不会将float解析为String ,那么为什么要使用DateTime呢?

这个永远不会失败:DateTime.Now.ToString(“yyyyMMdd HH:mm:ss.fff”)

Watch out if the date is DateTime.Min, as SQL Server uses 1700s for the start of time itself. 注意日期是否为DateTime.Min,因为SQL Server使用1700s作为开始时间本身。 I'd use an ISO datetime format : DateTime.ToString("s") but I haven't tried that on non-western installs. 我使用ISO日期时间格式:DateTime.ToString(“s”)但我没有在非西方安装上尝试过。

eg 例如

DateTime.Now.ToString("c")

is

insert into testtable values ('2009-01-22T15:08:13')

Formatting using DateTime.ToString("yyyy-MM-dd HH:mm:ss:fff") will match the MS SQL Server date/time format. 使用DateTime.ToString格式化(“yyyy-MM-dd HH:mm:ss:fff”)将与MS SQL Server日期/时间格式匹配。 (I believe SQL Server is fairly "intelligent" about recognizing slightly different-looking formats, eg with slashes, but I've personally used that one successfully.) (我相信SQL Server在识别略有不同的格式方面相当“聪明”,例如使用斜杠,但我个人成功地使用过它。)

EDIT: As noted by commenter, this will probably be ruined in some locales. 编辑:正如评论者所指出的,这可能会在某些语言环境中被破坏。

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

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