C#SQLite,读取后数据库保持锁定状态

问题描述:

我已经阅读了一些与此相关的文章,但不太了解.发生的情况似乎是,在对数据库进行此访问之后,该数据库将保持锁定状态,以便可以读取但不能将其写入.退出应用程序后,数据库将再次解锁.您能看看它并像我一样无知地跟我说话,指出我处理此问题的方式的错误吗?

I have read some of the related posts about this and didn't quite understand. What is happening is it appears that after this access to the database, the database is being left locked so that it can be read, but not written to. After exiting the application, the database is unlocked again. Can you look at it and talking to me like I am ignorant, point out the error of my way of handling this?

 public static Partner GetOnePartner(string code)
 {
    Partner partner = new Partner();
    SQLiteConnection connection = GroomwatchDB.GetConnection();
    string sqlStatement = "SELECT * FROM partners WHERE partner_code = @partner_code";

    SQLiteCommand command = new SQLiteCommand(sqlStatement, connection);
    command.Parameters.Add(new SQLiteParameter("@partner_code"));
    command.Parameters["@partner_code"].Value = code;

    try
    {
        connection.Open();
        SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow);
        if(reader.Read())
        {
            partner.Code = reader["partner_code"].ToString();
            partner.Last_name = reader["last_name"].ToString();
            partner.First_name = reader["first_name"].ToString();
            partner.Pay_rate = (double)reader["pay_rate"];
            partner.Active = reader["active"].ToString();
        }
        else
        {
            partner.Code = code;
            partner.Last_name = "Not Found";
        }

    }
    catch (SQLiteException ex)
    {
       throw ex;
    }
    finally
    {
        connection.Close();
    }

    return partner;

} 

您应该通过使用IDisposable模式正确使用连接.

You should correctly use your connection by using the IDisposable pattern.

实际上,每个实现IDisposable接口的类都需要使用using进行调用.这样可以确保调用方法Dispose(),从而清除了非托管资源(而且您不会以打开的文件结尾):

In fact, every classes that implements the IDisposable interface needs to be call with a using. This ensure that the methods Dispose() is call, and so the unmanaged resources are being cleared (and you don't end with an open file) :

public static Partner GetOnePartner(string code)
 {
    Partner partner = new Partner();
    string sqlStatement = "SELECT * FROM partners WHERE partner_code = @partner_code";
    using(SQLiteConnection connection = GroomwatchDB.GetConnection())
    using(SQLiteCommand command = new SQLiteCommand(sqlStatement, connection))
    {
        command.Parameters.Add(new SQLiteParameter("@partner_code"));
        command.Parameters["@partner_code"].Value = code;
        connection.Open();
        using(SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow))
        {
            if(reader.Read())
            {
                partner.Code = reader["partner_code"].ToString();
                partner.Last_name = reader["last_name"].ToString();
                partner.First_name = reader["first_name"].ToString();
                partner.Pay_rate = (double)reader["pay_rate"];
                partner.Active = reader["active"].ToString();
            }
            else
            {
                partner.Code = code;
                partner.Last_name = "Not Found";
            }
        }
    }
    return partner;
} 

参考文献:

  • Proper use of the IDisposable interface
  • Do I have to Dispose the SQLiteCommand objects?
  • SqlConnection SqlCommand SqlDataReader IDisposable