Can't rename SQLite database file after using it
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 开发者_StackOverflow中文版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?
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.
DBCommand
is IDisposable
. Maybe you need to call Dispose
on it or create it in a using
statement.
精彩评论