If you are worried about getting the format correctly, something is already seriously wrong. To work correctly with datetime values ββin any database, you need to do two things: not only sqlce:
- Make sure that the column is using the datetime type (not a text type like varchar).
- Make sure you use the datetime parameters in a parameterized query, and not in string concatenation.
If you do, formatting will not be performed on your part. At all. Example:
void SetDate(int recordID, datetime timeStamp) { string SQL = "UPDATE [sometable] SET someDateTimeColumn= @NewTime WHERE ID= @ID"; using (var cn = new SqlCeConnection("connection string here")) using (var cmd = new SqlCeCommand(SQL, cn)) { cmd.Parameters.Add("@NewTime", SqlDbType.DateTime).Value = timeStamp; cmd.Parameters.Add("@ID", SqlDbType.Integer).Value = recordID; cn.Open(); cmd.ExecuteNonQuery(); } }
Never ever EVER use string manipulations to replace values ββin sql queries. This is a huge no-no.
source share