是否可以使用SQLiteConnection库检查SQLite数据库是否被锁定

时间:2013-09-24 14:04:17

标签: c# sqlite

我正在使用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.");
        }

1 个答案:

答案 0 :(得分: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();
     }

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