2014-01-11 16 views
1

我登錄我的應用程序的活動,以小型文件數據庫(源碼)
日誌表只有3個字段:訂單,消息和時間戳的ID。
日誌條目顯示不那麼頻繁這樣我可以插入日誌記錄的正是時候。但是,我想在單獨的線程中執行查詢,我該怎麼做?我的意思是如何不「反應過度」?我只是不清楚纖維和線是什麼。這裏是我負責DB日誌記錄的班級:方式背景在C#中進行小sql查詢?

internal class LogEntryManager 
    { 
     private SQLiteConnection _sqlite; 
     public LogEntryManager() 
     { 
      if (!File.Exists(Environment.CurrentDirectory + @"\logs.db")) 
      { 
       SQLiteConnection.CreateFile("logs.db"); 
       _sqlite = new SQLiteConnection("Data Source=" + Environment.CurrentDirectory + @"\logs.db"); 
       _sqlite.Open(); 
       SQLiteCommand command = new SQLiteCommand("CREATE TABLE logs (Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, OrderId INTEGER NOT NULL, Message TEXT, Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)", _sqlite); 
       command.ExecuteNonQuery(); 
       _sqlite.Close(); 
      } 
      else 
       _sqlite = new SQLiteConnection("Data Source=logs.db"); 
     } 

     public void WriteLogEntry(int order, string message) 
     { 
      _sqlite.Open(); //Initiate connection to the db 
      SQLiteCommand cmd = _sqlite.CreateCommand(); 
      cmd.CommandText = String.Format("INSERT INTO logs (OrderId, Message) VALUES ({0}, {1});", order, message); 
      cmd.ExecuteNonQuery(); 
      _sqlite.Close(); 
     } 
    } 

謝謝你們!

回答

0

考慮使用Task隊列強大的後臺任務處理。