Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Encountered the same problem a while ago while writing a DB abstraction layer for C# and I never actually got around to finding out what the issue was. I just ended up throwing an exception when you attempted to delete a SQLite DB using my library.</p> <p>Anyway, this afternoon I was looking through it all again and figured I would try and find out why it was doing that once and for all, so here is what I've found so far.</p> <p>What happens when you call <code>SQLiteConnection.Close()</code> is that (along with a number of checks and other things) the <code>SQLiteConnectionHandle</code> that points to the SQLite database instance is disposed. This is done through a call to <code>SQLiteConnectionHandle.Dispose()</code>, however this doesn't actually release the pointer until the CLR's Garbage Collector performs some garbage collection. Since <code>SQLiteConnectionHandle</code> overrides the <code>CriticalHandle.ReleaseHandle()</code> function to call <code>sqlite3_close_interop()</code> (through another function) this does not close the database.</p> <p>From my point of view this is a very bad way to do things since the programmer is not actually certain when the database gets closed, but that is the way it has been done so I guess we have to live with it for now, or commit a few changes to System.Data.SQLite. Any volunteers are welcome to do so, unfortunately I am out of time to do so before next year.</p> <p><strong>TL;DR</strong> The solution is to force a GC after your call to <code>SQLiteConnection.Close()</code> and before your call to <code>File.Delete()</code>.</p> <p>Here is the sample code: </p> <pre><code>string filename = "testFile.db"; SQLiteConnection connection = new SQLiteConnection("Data Source=" + filename + ";Version=3;"); connection.Close(); GC.Collect(); GC.WaitForPendingFinalizers(); File.Delete(filename); </code></pre> <p>Good luck with it, and I hope it helps</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload