Delete sqlite db file

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); //error throws from here.                       
}

How can I prevent an error from occurring?

+3
source share
7 answers
+2

= False; .

+1
ThingzDatabase.localdb.conn.ClearAllPools(); 

, .

0

, -, API.

sqlite ? , ?

-1

procmon .
.
, , , .

-1
-1

, ,

  • overwriting the exisiting config or Setting.Setting file.

  • Clear all pools, .

  • Restart the application with Process.kill(name of your app exe)and then Process.start(your exe)withProcess class

  • Restarting will update the connection string and add exe with the new db.

  • Enumerate all db except the newly created in the target dirs and do whatever you want using FileInfo

-2
source

All Articles