I am developing a C# application with its backend as sqlite. In my application I have a button for cleaning the database (deleting the .db file and creating it again with some initial data). Sometimes when I try to delete the db it says it cannot be deleted because it is being used by another process. Before deleting it I am using close connection, dispose and clear pool functions. Even then it throws the same exception. Here is my code:
string targetDataBaseFilePath = Path.Combine(dataDirectoryPath, "local.db");
ThingzDatabase.Create(targetDataBaseFilePath, "touchdb");
In the create function I define the following code, from where I get the error:
if (File.Exists(DatabaseFileName))
{
try
{
ThingzDatabase.localdb.conn.Close();
ThingzDatabase.localdb.conn.Dispose();
SQLiteConnection.ClearAllPools();
}
catch (Exception)
{
}
File.Delete(DatabaseFileName); //error throws from here.
}
How can I prevent the error from being thrown?
ThingzDatabase.localdb.conn.ClearAllPools();
should be the call for that line I believe.