内存流作为数据库 [英] Memory Stream as DB
问题描述
我目前正在考虑使用 SQLite
作为我的 C# 项目的数据库引擎,但我遇到了以下问题:我找不到任何用于内存存储的 API.我想要实现的是以下内容:
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 want to achieve is the following:
在程序启动时,我想将 db 文件(从 HDD)加载到内存中.在程序执行期间,我想将此内存流用作真正的数据库(读取、写入、插入、选择等).关闭后将流保存到文件中.
Upon start of the program i want to load the db file (from HDD) into memory. During execution of the program i want to use this memory stream as a real db (read,write,insert,select etc). Upon closing save the stream to the file.
谁能以正确的方式指出我或建议另一个更适合此目的的数据库引擎.
Can anyone point me in the right way or suggest another db engine that would be better suited for this purpose.
推荐答案
您可以使用 SQLite Online Backup API能够将 db 文件复制到内存,将内存复制到文件.对 SQLite Online Backup API 的本机支持存在于 System.Data.SQLite 从 1.0.80.0 版(使用 SQLite 3.7.11).
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).
这是如何在 C# 中使用 API 的简单示例:
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();
}
这篇关于内存流作为数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!