2013-09-24 100 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

相关问题