13
我目前正在考虑使用SQLite
作为我的C#项目的数据库引擎,但我遇到了以下问题:我无法找到任何用于内存存储的API。我想要实现的是以下几点:作为数据库的内存流
程序启动时,我想将db文件(从硬盘)加载到内存中。 在程序执行过程中,我想把这个内存流作为一个真正的数据库(读,写,插入,选择等)。 关闭后将流保存到文件。
任何人都可以用正确的方式指出我的意思,或者建议另一个更适合此目的的数据库引擎。
我目前正在考虑使用SQLite
作为我的C#项目的数据库引擎,但我遇到了以下问题:我无法找到任何用于内存存储的API。我想要实现的是以下几点:作为数据库的内存流
程序启动时,我想将db文件(从硬盘)加载到内存中。 在程序执行过程中,我想把这个内存流作为一个真正的数据库(读,写,插入,选择等)。 关闭后将流保存到文件。
任何人都可以用正确的方式指出我的意思,或者建议另一个更适合此目的的数据库引擎。
您可以使用SQLite Online Backup API有能力将db文件复制到内存,内存到文件。 SQLite Online Backup API的本地支持存在于版本1.0.80.0(含SQLite 3.7.11)的System.Data.SQLite中。
这是简单的例子,如何API可以在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();
}
谢谢你,这个漂亮和干净的解决方案。 – Anonymous 2012-07-08 20:40:34
有没有办法做同样的事情来获得字节[]而不接触磁盘? – Gleno 2013-08-31 22:57:54
警告:对内存数据库*使用备份API将删除[耐久性](http://en.wikipedia.org/wiki/Durability_(database_systems))保证*。 – user2864740 2014-09-15 19:51:09