SQLite Shared Cache Database

1.4k Views Asked by At

I had created a shared in-memory database using the following code, but the problem is, even I close the connection or restart the machine, the data is still available.

I didn't understand how the data is persistent and where it is physically stored.

using (SQLiteConnection database = new SQLiteConnection("file: empDB ? mode = memory & cache = shared"))
            {
                database.CreateTable(emp.GetType());
                database.Insert(emp);
                var value = database.Query<Emp>("select * from emp;select * from emp;");
            }
1

There are 1 best solutions below

2
On BEST ANSWER

To achieve expected behavior you could use FullUri syntax, e.g:

using (SQLiteConnection database = new SQLiteConnection("FullUri=file:empDB?mode=memory&cache=shared"))
{
  database.Open();
  // ....
}

I didn't understand how the data is persistent and where it is physically stored.

Database file with name empDB is created in your application working directory when you use your original connection string, e.g. FullUri=file: empDB ? mode = memory & cache = shared. This file is reused when you run the application again.