3

我正在使用 SQLiteConnection 访问 Firefox 生成的 sqlite 文件。我的目标是使用这个 C# 控制台应用程序来查找所有已安装的扩展。

这是问题所在,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.");
        }
4

1 回答 1

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();
     }

您可以尝试将代码更改为上面的代码吗?

于 2013-09-24T14:48:46.460 回答