What is the formatting difference between:
DateTime datetimenow = DateTime.Now.ToLocalTime();
and
long ticks = 634687637580000000;
DateTime createdTimestamp = new DateTime(ticks).ToLocalTime();
Which causes SQL Server CE to throw error
There was an error in a part of the date format. [ Expression (if
known) = ]
I am attempting to insert two timestamps into a SQL Server CE database. The current date time and a date time from the past. The current date and time (now) can be inserted into both target fields in the table, however when I use the createdTimestamp the error above is thrown.
The format when I review the locals in debug are exactly the same between the two.
The target fields are both of datetime data type.
Why might this error be thrown?
Edit:
I’ve just found it appears to only affect timestamps in the past. Now and future timestamps work fine.
Edit: adding code for SQL insertion.
Query is built dynamically by my own SQL Server CE class.
{INSERT timeline (TweetID,Author,TimestampOfTweet,TimestampOfCollected,TweetText) VALUES ('185941815562739713','twitterapi','15/12/2011 13:52:39','01/04/2012 15:58:58','TweetDeck access - Update : This issue has been resolved. http://t.co/2yub0tYL')}
Execution Code:
//Take built statement and apply to command text
cmd.CommandText = buildSqlStatement.ToString();
//execute insert
try
{
cmd.Connection = sqlConn;
sqlConn.Open();
int rows = cmd.ExecuteNonQuery();
string results = Convert.ToString(rows);
return results;
}
catch(Exception ex)
{
return ex.Message;
}
finally
{
sqlConn.Close();
}
Never include values directly in your SQL – always use parameterized SQL instead.
Benefits: