简体   繁体   English

SQLite 内存数据库不适用于 Microsoft Store

[英]SQLite in-memory database not working with Microsoft Store

I am writing an application that creates and uses a (temporary) in-memory SQLite Database.我正在编写一个创建和使用(临时)内存中 SQLite 数据库的应用程序。 My local debug/release copies, as well as the ClickOnce version, work fine, but when I use the version deployed/downloaded from the Microsoft Store, I get an unable to open database file error.我的本地调试/发布副本以及 ClickOnce 版本工作正常,但是当我使用从 Microsoft Store 部署/下载的版本时,我收到unable to open database file错误。

This is my connection string:这是我的连接字符串:

Data Source=InMemoryDB;Mode=Memory;Cache=Shared;

I imagine it might have something to do with SQlite creating a file on disk somewhere and then not being able to access it because it's in a protected folder?我想这可能与 SQlite 在某处的磁盘上创建一个文件然后因为它位于受保护的文件夹中而无法访问它有关? But I don't know how I would specify (change) this since I am using an in-memory database.但是我不知道如何指定(更改)它,因为我使用的是内存数据库。

As suspected, implicitly, even though this is an in-memory database, SQLite still needs to create a dummy file somewhere for shared access.正如怀疑的那样,隐含地,即使这是一个内存数据库,SQLite 仍然需要在某处创建一个虚拟文件以进行共享访问。 It treats the Data Source parameter implicitly as a path to this file.它将Data Source参数隐式视为此文件的路径。 When a full path isn't specified, it treats this as the file name and tries to write to the local folder.如果未指定完整路径,它会将其视为文件名并尝试写入本地文件夹。

In the case of Windows Store, the app doesn't have write access to the local folder, and thus this file cannot be created.对于 Windows 应用商店,应用程序没有本地文件夹的写入权限,因此无法创建此文件。 Thus the solution is to specify a full path to a temporary file instead of just InMemoryDB .因此,解决方案是指定临时文件的完整路径,而不仅仅是InMemoryDB

public static string tempDBFile = Path.GetTempFileName();
...
var connString = $"Data Source={tempDBFile};Mode=Memory;Cache=Shared;";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM