简体   繁体   中英

SQLite in-memory database not working with Microsoft Store

I am writing an application that creates and uses a (temporary) in-memory SQLite Database. 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.

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? 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. It treats the Data Source parameter implicitly as a path to this file. 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. Thus the solution is to specify a full path to a temporary file instead of just InMemoryDB .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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