在C#中,有没有办法将内存中的文件作为内存中的SQLite数据库链接到System.Data.SQLite?

在C#中,有没有办法将内存中的文件作为内存中的SQLite数据库链接到System.Data.SQLite?,c#,sqlite,connection,system.data.sqlite,in-memory-database,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.Lengt

我想做的事情大致如下:

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
}

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

如果您不介意使用互操作并直接转到(然后将返回的句柄包装到文件流中)您可以看看如何创建指定了file_ATTRIBUTE_TEMPORARY的文件,只要有缓存可用,它就不会将文件写入磁盘,并且在文件句柄关闭时会自动删除该文件

如果有足够的缓存可用,则指定FILE_属性_TEMPORARY属性会导致文件系统避免将数据写回大容量存储器,因为应用程序会在句柄关闭后删除临时文件。在这种情况下,系统完全可以避免写入数据。尽管它没有以与前面提到的标志相同的方式直接控制数据缓存,但FILE_属性_TEMPORARY属性确实告诉系统在不写入的情况下尽可能多地保存在系统缓存中,因此可能会引起某些应用程序的关注


使用Interop和CreateFile()太复杂了。下面是一种使用file_ATTRIBUTE_TEMPORARY属性创建文件的简单方法,该属性将尽可能使用缓存:

var file = File.Create(path);
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Temporary);

您可以在内存数据库中使用SQLite并通过SQLite对其进行操作,也可以将内存流写入临时文件并打开它。在连接到该文件之前,您需要“处理该文件”哪些事情?我希望能够编辑该流。除非上述方法可行,否则我的理由并不重要。