2013-09-24 98 views
3

我正在使用SQLiteConnection訪問Firefox生成的sqlite文件。我的目標是使用這個C#控制檯應用程序來查找所有已安裝的擴展。是否有可能使用SQLiteConnection庫檢查SQLite數據庫是否被鎖定

下面是問題,Firefox首次啓動時會鎖定數據庫,並且每當我安裝新擴展時也會如此。當我重新啓動Firefox時它會解鎖數據庫,但我想甚至在鎖定時甚至不嘗試從數據庫讀取數據。

正如我的問題所問,是否有可能檢查數據庫是否被鎖定?

下面是從數據庫中讀取一些代碼:

if (File.Exists(profilePath)) 
     { 
      //Connect to the DB. 
      string connectionPath = @"Data Source=" + profilePath; 
      using (SQLiteConnection connection = new SQLiteConnection(connectionPath)) 
      { 
       //Create the command to retrieve the data. 
       SQLiteCommand command = connection.CreateCommand(); 
       //Open the connection. 
       try 
       { 
        connection.Open(); 
        System.Diagnostics.Debug.WriteLine("Connection Reuslt Code: " + connection.ResultCode()); 
        System.Diagnostics.Debug.WriteLine("Connection State: " + connection.State.ToString()); 

        //Get the tables. 
        DataTable tables = connection.GetSchema("Tables"); 
        //Prepare the query to retreive all the add ons. 
        string query = "SELECT name as 'Extension_Name', id as 'extID', updateDate as 'Date' FROM addon"; 
        command.CommandText = query; 
        command.ExecuteNonQuery(); 

        //Retreive the dataset using the command. 
        SQLiteDataAdapter da = new SQLiteDataAdapter(command); 
        DataSet ds = new DataSet(); 
        da.Fill(ds, "addon"); 

        for (int i = 0; i < ds.Tables["addon"].Rows.Count; i++) 
        { 
         //Get the date from the query. 
         long entryDateTicks = (long)ds.Tables["addon"].Rows[i].ItemArray[2]; 
         //Add the ticks to the unix date. Devide by 1000 to convert ms to seconds. 
         DateTime unixTime = new DateTime(1970, 1, 1); 
         DateTime entryDate = unixTime.AddSeconds(entryDateTicks/1000); 

         //Add the data to a list/ 
         addonList.Add(new FirefoxAddonEntry(
          ds.Tables["addon"].Rows[i].ItemArray[0].ToString() 
          , ds.Tables["addon"].Rows[i].ItemArray[1].ToString() 
          , entryDate 
          )); 
        } 
       } 
       catch 
       { 
        System.Diagnostics.Debug.WriteLine("Error when opening the firefox sqlite file."); 
       } 
       finally 
       { 
        connection.Close(); 
       } 
      } 
     } 
     else 
     { 
      System.Diagnostics.Debug.WriteLine("The extensions file does not exists."); 
     } 
+0

你想使用FireFox它的數據庫嗎? – Max

+1

如果我錯過了對話,我很抱歉,但是如果您問我是否想在從其數據庫讀取數據時使用Firefox,答案是肯定的。 – Garrett

+0

不可能讀/寫fireFox數據庫,你應該爲插件/擴展本身創建一個數據庫,這樣你就可以確定它被鎖定的時間是或不是。 – Max

回答

2

交易看起來如下:

 using (TransactionScope tran = new TransactionScope()) 
    { 
        connection.Open(); 
        System.Diagnostics.Debug.WriteLine("Connection Reuslt Code: " + connection.ResultCode()); 
        System.Diagnostics.Debug.WriteLine("Connection State: " +     connection.State.ToString()); 

        //Get the tables. 
        DataTable tables = connection.GetSchema("Tables"); 
        //Prepare the query to retreive all the add ons. 
        string query = "SELECT name as 'Extension_Name', id as 'extID', updateDate as 'Date' FROM addon"; 
        command.CommandText = query; 
        command.ExecuteNonQuery(); 

        //Retreive the dataset using the command. 
        SQLiteDataAdapter da = new SQLiteDataAdapter(command); 
        DataSet ds = new DataSet(); 
        da.Fill(ds, "addon"); 

        for (int i = 0; i < ds.Tables["addon"].Rows.Count; i++) 
        { 
         //Get the date from the query. 
         long entryDateTicks = (long)ds.Tables["addon"].Rows[i].ItemArray[2]; 
         //Add the ticks to the unix date. Devide by 1000 to convert ms to seconds. 
         DateTime unixTime = new DateTime(1970, 1, 1); 
         DateTime entryDate = unixTime.AddSeconds(entryDateTicks/1000); 

         //Add the data to a list/ 
         addonList.Add(new FirefoxAddonEntry(
          ds.Tables["addon"].Rows[i].ItemArray[0].ToString() 
          , ds.Tables["addon"].Rows[i].ItemArray[1].ToString() 
          , entryDate 
          )); 
        } 

     tran.Complete(); 
    } 

你能試着改變你的代碼上面的代碼?

+0

你從哪裏找到TransactionScope類?這是不同的圖書館嗎? – Garrett

+0

右鍵單擊TransactionScope - > Resolve - >上層項目。 – Max

+0

我應該提到我卡住使用.NET 2.0 – Garrett

相關問題