I'm writing simple text log Entries to an SQL-table like so:
private SqlConnection sqlcon;
// ...
try {
sqlcon.Open();
SqlCommand cmd = sqlcon.CreateCommand();
cmd.CommandText = this.logString;
cmd.ExecuteNonQuery();
}
/* catch SqlException, InvalidOperationException, Exception */
/* finally sqlcon.Close() */
Asynchronous Processing is set to false, so i thought the log entries would appear in the table in the order of execution. But that is not the case, if 2 or more log entries are fired in about less than 5ms.
So my guess is that ExecuteNonQuery runs on a different thread and somehow the different events get mixed up. I've already tried using Asynchronous Processing with BeginExecuteNonQuery and EndExecuteNonQuery, but it messes up my code and is not really working anyway.
So my question is: Is there any way to ensure, that the nonQueries are being executed in exactly the order they were triggered?
edit: Maybe it's important, i'm using a timestamp like so
cmd.Parameters.Add("timestamp", SqlDbType.DateTime ).Value = System.DateTime.Now;
What if you use SQL Server SYSDATETIME to insert timestamps? I think that ExecuteNonQuery
is synchronous, but maybe something wrong with your timestamp.
There is a good document about time resolution in Windows.