将DateTime解析到SQL服务器的最佳方式 [英] Best way to parse DateTime to SQL server
问题描述
我想知道什么是最好的方法来解析一个DateTime对象到你的SQL服务器。
I was wondering what is the best way to parse a DateTime object to your SQL server.
在代码中生成SQL的位置。
Where you are generating the SQL in code.
我一直使用类似 DateTime.Now.TolongDateString()
的内容,并取得了很好的成绩,除了今天以外我有一个错误,这让我想起了。
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.
System.Data.SqlClient.SqlException: Conversion failed when converting datetime from character string
那么大家的想法和建议是什么,一个方法将工作acrss所有的SQL服务器无论什么设置..
So what is everyone thoughts and recomendations for a way that will work acrss all SQL server no matter what there setup..
也许像 DateTime.Now.ToString(yyyy / MM / dd)
推荐答案
只有2种安全格式
ISO和ISO8601
ISO and ISO8601
ISO = yymmdd
ISO = yymmdd
ISO8601 = yyyy-mm-dd Thh:mm:ss:mmm(无空格)(注意T)
ISO8601 = yyyy-mm-dd Thh:mm:ss:mmm(no spaces) (notice the T)
另请参见:为SQL Server设置标准DateFormat
这篇关于将DateTime解析到SQL服务器的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!