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 with some source data). Sometimes, when I try to delete db, it says that it cannot be deleted because it is being used by another process. Before deleting, I use a tight connection, arrange and clear the pool functions. Even then, it raises 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);
}
How can I prevent an error from occurring?
source
share