I listen to the data stream and save the data as insert statements in ConcurrentQueue and insert the data with bulk insert using System.Threading.Timer with an interval of 1000. The whole script runs on a static class. Here is the code:
static void timer_Elapsed(object sender, ElapsedEventArgs e) { if (queryQueue.IsEmpty) return; string text = ""; //bulkBuilder is StringBuilder. //queryQueue is ConcurrentQueue bulkBuilder.AppendLine("PRAGMA synchronous = 0;PRAGMA count_changes = FALSE;PRAGMA journal_mode=OFF;Begin;"); while (queryQueue.TryDequeue(out text)) { bulkBuilder.Append(text); bulkBuilder.AppendLine(";"); } bulkBuilder.AppendLine("Commit;"); try { sqlCommand.CommandText = bulkBuilder.ToString(); sqlCommand.ExecuteNonQuery(); } catch (System.Exception ex) { Console.WriteLine("Error while inserting Data : " + ex.Message); } finally { bulkBuilder.Clear(); } }
Funny, sqlCommand used only for insertion, just ExecuteNonQuery() in this timer. And from time to time the error message "Unable to install command text while datareader is active" appears. This is absurd, because this code has nothing to do with the internal SQLiteDataReader in sqlCommand .
How can I get rid of this error?
source share