I am not able to convert the c# date time7/31/2017 3:13:49 PM to SQL date time while inserting the records to the database.
I am doing this using
DateTime dt = DateTime.Parse("11/23/2010");
string toSqlDate= dt.ToString("yyyy-MM-dd HH:mm:ss");
DateTime finalDate = DateTime.Parse(toSqlDate);
But it's giving me the error.
String was not recognized as a valid DateTime.
The .Net DateTime
maps directly to SQL Server DateTime
. This means you don't need to worry about the display format at all, since DateTime
does not have a display format.
All you need to do is pass the instance of the DateTime
struct as a parameter to the SqlCommand
:
SqlCommand.Parameters.Add("@DateTimeParam", SqlDbType.DateTime).Value = dt;
Bonus reading: How do I create a parameterized SQL query? Why Should I?