This seems like it should be really obvious, but how can I put a DateTime object into an MSSQL database? When I convert it to a string, it keeps adding "-7:00" at the end for the time zone offset and so the query isn't accepted. How can I fix this?
2 Answers
How are you trying to insert the DateTime into the database? If you're converting it to a string to be passed into a stored procedure (bad idea; better to use the date SQL type), then you should first convert all DateTime objects to UTC using the .net method ToUniversalTime. Once in UTC, the DateTime will have no timezone offset.
Assuming you are using C#, I would suggest the following:
SqlCommand cmd = new SqlCommand("INSERT INTO yourTable (dateTimeColumn) VALUES (@value)", connection);
cmd.Parameters.AddWithValue("@value", yourDateTimeObject);
cmd.ExecuteNonQuery();
This will work for inserting the value. If not, please post your code to show where the error is because that means that the datetime object you are getting your value from is passing in the data wrong.