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 に答える