C# SQLite, database being left locked after a read

前端 未结 1 541
[愿得一人]
[愿得一人] 2021-01-24 12:52

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

相关标签:
1条回答
  • 2021-01-24 13:28

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

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

    References :

    • Proper use of the IDisposable interface
    • Do I have to Dispose the SQLiteCommand objects?
    • SqlConnection SqlCommand SqlDataReader IDisposable
    0 讨论(0)
提交回复
热议问题