Memory Stream as DB

后端 未结 1 526
北恋
北恋 2020-12-07 14:45

I\'m currently thinking of using SQLite as db engine for my C# project, but i ran into the following problem: i can\'t find any API for memory storage. What i w

相关标签:
1条回答
  • 2020-12-07 15:13

    You can use SQLite Online Backup API that has ability to copy db file to memory, memory to file. Native support for SQLite Online Backup API is present in System.Data.SQLite from version 1.0.80.0 (with SQLite 3.7.11).

    This is simple example how API can be used in C#:

    SQLiteConnection source = new SQLiteConnection("Data Source=c:\\test.db");
    source.Open();
    
    using (SQLiteConnection destination = new SQLiteConnection(
      "Data Source=:memory:"))
    {
      destination.Open();               
    
      // copy db file to memory
      source.BackupDatabase(destination, "main", "main",-1, null, 0);
      source.Close();
    
      // insert, select ,...        
      using (SQLiteCommand command = new SQLiteCommand())
      {
        command.CommandText =
          "INSERT INTO t1 (x) VALUES('some new value');";
    
        command.Connection = destination;
        command.ExecuteNonQuery();
      }             
    
      source = new SQLiteConnection("Data Source=c:\\test.db");
      source.Open();
    
      // save memory db to file
      destination.BackupDatabase(source, "main", "main",-1, null, 0);
      source.Close();               
    }
    
    0 讨论(0)
提交回复
热议问题