Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite locking file even though connection is closed

Tags:

c#

sqlite

Shouldn`t the following statement be autocommited? I get an IOException trying to delete the file after executing the query.

using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "data\\test.db;Version=3;"))
{
    connection.Open();
    SQLiteCommand command = new SQLiteCommand("CREATE TABLE IF NOT EXISTS test (id INTEGER)", connection);
    command.ExecuteNonQuery();
}

//throwing an IOException
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "data\\test.db");
like image 735
Chris Ortiz Avatar asked May 06 '10 08:05

Chris Ortiz


3 Answers

I'm late to the party, but I had to:

GC.WaitForPendingFinalizers();
GC.Collect();

Prior to calling File.Delete().

like image 179
joelc Avatar answered Oct 30 '22 14:10

joelc


Isn't the SQLiteCommand also Dispose(able)? The connection might not close because you haven't closed the command yet. I'd use a using for that too.

like image 45
Joel Lucsy Avatar answered Oct 30 '22 15:10

Joel Lucsy


You have to dispose of every SQLiteConnection, SQLiteCommand and SQLiteDataReader once you are done using it. Simply create the SQLiteCommand within a "using" block and the problem will go away.

like image 27
Captain Sensible Avatar answered Oct 30 '22 14:10

Captain Sensible