-2

I have a slight problem that I'm trying to solve. I am using SQLite database and i created my database Schedule.db automatically when application starts for the first time(if .db does not already exists).

On a button_click I want to delete it so I can create new one when I start my application again.

The problem is, every time I try to delete it I get an error:

"Additional information: The process cannot access the file '/filePath.../Scheduler.db' because it is being used by another process."

I understand that I can't delete it because my application is already using it but is there any solution to my current problem?

 string databasePath = AppDomain.CurrentDomain.BaseDirectory + "Scheduler.db";

        if (MessageBox.Show("Do you want to delete database: [Scheduler.db]?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes) == MessageBoxResult.Yes)
        {
            if (File.Exists(databasePath))
            { 

                SQLiteConnection connectionSqlLIte = new SQLiteConnection(@"Data Source=Scheduler.db;Version=3;"); 
                connectionSqlLIte. Close();

                File.Delete(databasePath);
                MessageBox.Show("Database deleted: [Scheduler] ");
                Application.Current.Shutdown();  

            }
            else
            {
                MessageBox.Show("There is no database: [Scheduler]!");
            }
        }
SiGe
  • 341
  • 6
  • 18
Snailtamer
  • 21
  • 9
  • 2
    Do you have open connections to the database? Show us how you 'close' connections in code – Grantly Dec 14 '17 at 12:42
  • SQLiteConnection connectionSqlLIte = new SQLiteConnection(@"Data Source=Scheduler.db;Version=3;"); connectionSqlLIte.Open/Close(); If this is what you meant by your question. I dont know if i have to close my connection before i delete my database. – Snailtamer Dec 14 '17 at 12:52
  • Please add your code snippet to your question....Yes you absolutely must close your connection before you delete your database...But there may be more required...We need to see the code in your question :)' – Grantly Dec 14 '17 at 12:57
  • well, closing a connection before deleting database seems like a reasonable thing to do :) I added connectionSqLite.Close() in If() statement but it does not work so i guess something more is needed to be done, is shows the same error. Ill try my best. Thanks Grantly :) – Snailtamer Dec 14 '17 at 13:06
  • Yeah it will require more than just Close, but that is a start :) Edit your question and add your 'close' function so we can all see it, and answer with additional code that will sever your connection and allow the db to be deleted – Grantly Dec 14 '17 at 13:11
  • I hope i put Close() in the right spot before trying to delete .db. Dont know if i have to put Dispose() too after i close the connection :/ – Snailtamer Dec 14 '17 at 13:17

2 Answers2

0

I'm flying a bit blind here, without your exact code but this is worth trying:

...
connectionSqLite.Close();
connectionSqLite.Dispose();
connectionSqLite = null;   // Bad coding, see if you can delete this line
...

Now it is possible that the database is free to be deleted (and recreated)

You should not have to set connectionSqLite = null, this is overkill and usually unnecessary and also kinda incorrect, but try it out anyway. See here

(But - remember that after you have called Dispose on the connectionSqLite object - it is essentially empty (null). So you must set it to a new instance of your object before you can use it again)

Grantly
  • 2,546
  • 2
  • 21
  • 31
  • That is the whole code from my button_click. I dont see that i have "**connectionSqLite = null**" in my code but added Dispose() after i Close() and still not working :/ Anyway, i gave up. I simply cant make it work the way i want it to work. – Snailtamer Dec 15 '17 at 07:55
0

Just force a garbage collection after the connection is closed like this:-

string databasePath = AppDomain.CurrentDomain.BaseDirectory + "Scheduler.db";

        if (MessageBox.Show("Do you want to delete database: [Scheduler.db]?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes) == MessageBoxResult.Yes)
        {
            if (File.Exists(databasePath))
            { 

                SQLiteConnection connectionSqlLIte = new SQLiteConnection(@"Data Source=Scheduler.db;Version=3;"); 
                connectionSqlLIte. Close();

                //Force a garbage collection here
                GC.Collect();
                GC.WaitForPendingFinalizers();

                //Now you should be able to delete the file now
                File.Delete(databasePath);
                MessageBox.Show("Database deleted: [Scheduler] ");
                Application.Current.Shutdown();  

            }
            else
            {
                MessageBox.Show("There is no database: [Scheduler]!");
            }
        }


Jon
  • 190
  • 2
  • 15