将 DateTime 解析为 SQL 服务器的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/469417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to parse DateTime to SQL server
提问by TheAlbear
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 Server 中工作的方式,每个人的想法和建议是什么..
Maybe something like DateTime.Now.ToString("yyyy/MM/dd")
也许像 DateTime.Now.ToString("yyyy/MM/dd")
回答by SQLMenace
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 设置标准日期格式
回答by Netricity
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 的说法,这是通用的方式。
回答by Kev
Why not parameterise the query and pass the DateTime value as a SQL DateTime input param?
为什么不参数化查询并将 DateTime 值作为 SQL DateTime 输入参数传递?
e.g 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,您也应该能够专门处理这个问题吗?
回答by kristof
You should really use parametrized queries for that and pass your DateTimeobject as a SQL DateTimeparameter.
您真的应该为此使用参数化查询,并将您的DateTime对象作为SQL DateTime参数传递。
If you parse the DateTimeto Stringyou will have to deal with the localisation settings both on your application server as on the database server. That can lead to some nasty surprises like treating the Date as it was in US format on one side and e.g. UK on another. The string 9/12/2000 can be either September the 12th or the 9th of December. So keep the data in the DateTimeobject/type when exchanging between application and database.
如果您将DateTime解析为String,您将不得不像在数据库服务器上一样处理应用程序服务器上的本地化设置。这可能会导致一些令人讨厌的意外,例如在一侧处理日期为美国格式,而在另一侧处理英国格式。字符串 9/12/2000 可以是 9 月 12 日或 12 月 9 日。因此,在应用程序和数据库之间交换时,将数据保留在DateTime对象/类型中。
The only time you would parse DateTimeto Stringwould be when displaying data (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.
将DateTime解析为String的唯一时间是在显示数据 (GUI) 时。但是你应该确保在解析时使用正确的本地化设置以用户期望的格式显示它。
The same principle applies to other data types like floatfor example. The string representation of this varies depending on the locale and I suppose you do not parse floatto Stringwhen passing it to the database so why do it with the DateTime?
相同的原则适用于其他数据类型,例如float。它的字符串表示因区域设置而异,我想您在将它传递给数据库时不会将float解析为String那么为什么要使用DateTime呢?
回答by Mladen Prajdic
this one won't ever fail: DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff")
这个永远不会失败:DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff")
回答by Chris S
Watch out if the date is DateTime.Min, as SQL Server uses 1700s for the start of time itself. I'd use an ISO datetime format : DateTime.ToString("s") but I haven't tried that on non-western installs.
注意日期是否为 DateTime.Min,因为 SQL Server 使用 1700 秒作为时间本身的开始。我会使用 ISO 日期时间格式:DateTime.ToString("s") 但我还没有在非西方安装中尝试过。
e.g.
例如
DateTime.Now.ToString("c")
is
是
insert into testtable values ('2009-01-22T15:08:13')
回答by mqp
Formatting using DateTime.ToString("yyyy-MM-dd HH:mm:ss:fff") will match the MS SQL Server date/time format. (I believe SQL Server is fairly "intelligent" about recognizing slightly different-looking formats, e.g. with slashes, but I've personally used that one successfully.)
使用 DateTime.ToString("yyyy-MM-dd HH:mm:ss:fff") 格式化将匹配 MS SQL Server 日期/时间格式。(我相信 SQL Server 在识别外观略有不同的格式方面相当“智能”,例如使用斜杠,但我个人已经成功地使用了这种格式。)
EDIT: As noted by commenter, this will probably be ruined in some locales.
编辑:正如评论者所指出的,这可能会在某些语言环境中被破坏。