Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

.net - 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.

Where you are generating the SQL in code.

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

So what is everyone thoughts and recomendations for a way that will work acrss all SQL server no matter what there setup..

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

there are only 2 safe formats

ISO and ISO8601

ISO = yymmdd

ISO8601 = yyyy-mm-dd Thh:mm:ss:mmm(no spaces) (notice the T)

See also here: Setting a standard DateFormat for SQL Server


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...