1

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;");
            }
Munna Extreme
  • 390
  • 1
  • 13

1 Answers1

2

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.

Nikita
  • 6,270
  • 2
  • 24
  • 37
  • thanks, I found a file in working directory i.e is holding the data. but it shows file size is always 0, but size on disk is kept increasing whenever new data adds. seems weird. – Munna Extreme Aug 23 '18 at 05:13
  • Data is not written immediatly to the drive. It's kept in write cache in RAM to reduce number of hard disk IO operations. You can manually flush such cache with use of [Sync](https://learn.microsoft.com/ru-ru/sysinternals/downloads/sync) utility on Windows. – Nikita Aug 23 '18 at 09:53