gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:03:07 27 4
gpt4 key购买 nike

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

最佳答案

交易看起来如下:

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

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

关于c# - 是否可以使用 SQLiteConnection 库检查 SQLite 数据库是否被锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18983838/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com