views:

155

answers:

2

I'm trying to rename my SQLite database file after I'm done using it, however the file still appears to be opened by SQLite even after all my connections are closed.

Here is an example of what I'm doing:

using (DbConnection conn = new SQLiteConnection("Data Source=test.db"))
{
    conn.Open();
    DbCommand command = conn.CreateCommand();
    command.CommandText = "select id from test";
    command.ExecuteScalar();
}
File.Move("test.db", "test.db.test");

The Move call throws an IOException. This is the only connection that I have to this database file. Once the application ends I can move the file manually without a problem. I've tried various things such as explicitly setting Pooling=false in the connection string, manually calling Close before the connection is disposed, explicitly starting and committing a transaction, but none of this seems to help. Is there a way to force SQLite to close/release the database file without exiting the application?

A: 

I presume you are using SQLite.Net? I have just tried your code and works without problem with SQLite.Net 1.0.65, in VS 2005 on Vista.

vit
Yes, I'm using that library and that version. I'm using VS2008 (still with .NET 2.0 though). I wonder what is going on? Did you create the database file beforehand to include the 'test' table? I tried that same code without a preexisting database file and simply caught the exception thrown by the ExecuteScalar call and the move worked with no problem. Although, the database file is 0 bytes so that's not really an accomplishment...
Dennis
Yes, I used the same code to ExecuteNonQuery of "create table test (id integer)" first, then I changed the code back and run it several time in debug and release modes. Could it be that this file is used somewhere else in the code or by some external tool?
vit
I just redownloaded SQLite.NET and now it's working fine, that's very strange. Thanks for trying this out as I probably never would have figured this out on my own.
Dennis
A: 

DBCommand is IDisposable. Maybe you need to call Dispose on it or create it in a using statement.

dangph