c# - 在 C# 中,有什么方法可以将内存中的文件作为内存中的 SQLite 数据库与 System.Data.SQLite 链接?

标签 c# sqlite connection system.data.sqlite in-memory-database

我想做的是以下几件事:

using System.Data.SQLite;
using System.IO;

//My SQLite connection
SQLiteConnection myCon;

public void ReadAndOpenDB(string filename)
{
    FileStream fstrm = new FileStream(filename, FileMode.Open);
    byte[] buf = new byte[fstrm.Length];
    fstrm.Read(buf, 0, (int)fstrm.Length);
    MemoryStream mstrm = new MemoryStream(buf);

    //Do some things with the memory stream

    myCon = new SQLiteConnection(/*attach to my memory stream for reading*/);
    myCon.Open();

    //Do necessary DB operations
}

我不打算写入内存中的数据库,但在连接到它之前,我需要能够在我的程序的内存中对文件执行一些操作。

最佳答案

如果您不介意使用 Interop 并转到 CreateFile()直接(然后将返回的 HANDLE 包装在 FileStream 中)你可以看看创建指定 FILE_ATTRIBUTE_TEMPORARY 的文件,只要有可用的缓存内存,它就不会将文件写入磁盘,并且当它的句柄是时自动删除文件关闭。

Specifying the FILE_ATTRIBUTE_TEMPORARY attribute causes file systems to avoid writing data back to mass storage if sufficient cache memory is available, because an application deletes a temporary file after a handle is closed. In that case, the system can entirely avoid writing the data. Although it does not directly control data caching in the same way as the previously mentioned flags, the FILE_ATTRIBUTE_TEMPORARY attribute does tell the system to hold as much as possible in the system cache without writing and therefore may be of concern for certain applications.

关于c# - 在 C# 中,有什么方法可以将内存中的文件作为内存中的 SQLite 数据库与 System.Data.SQLite 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6191155/

相关文章:

android - 加载器框架与普通 AsyncTask

c# - MonoTouch 和 SQLite - 先前成功连接后无法打开数据库

sqlite - Ionic2:使用SQLite,Ionic服务

c# - 如何从 C# 作为 MT4 dll 连接到 Mysql?

Java <-> Python : share objects

c# - 通用类型约束 : How do I create an instance of the class described in this C# documentation

c# - 在 MiniProfiler 中显示自定义消息

reactjs - 无法从我的移动设备连接到我的 React 应用程序

c# - Windows Phone 7 ListPicker InvalidCastException

c# - 在 reportViewer 中的 .rdlc 报告之间切换