嗨,有人可以推荐我如何在 Windows 商店应用程序中连接到 sql lite db 吗?我将名为 database.sqlite 的文件添加到我的应用程序中,就像新项目一样。但我找不到如何连接到现有文件。我发现这个:
StorageFile DataFile =
await ApplicationData.Current.LocalFolder.GetFileAsync("database.sqlite");
但我不知道如何写正确的文件路径。有没有人有一些想法。
嗨,有人可以推荐我如何在 Windows 商店应用程序中连接到 sql lite db 吗?我将名为 database.sqlite 的文件添加到我的应用程序中,就像新项目一样。但我找不到如何连接到现有文件。我发现这个:
StorageFile DataFile =
await ApplicationData.Current.LocalFolder.GetFileAsync("database.sqlite");
但我不知道如何写正确的文件路径。有没有人有一些想法。
将数据库文件添加到项目时,它被视为项目的资产。该文件与您的应用程序一起存储,并且位于只读位置。
要访问只读版本,您必须使用以下命令:
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///relative/path/to/db/database.sqlite"));
relative/path/to/db
项目顶层的相对路径在哪里。例如,如果您的项目有一个名为的文件夹Data
,其中包含您的 db 文件,那么您将使用"ms-appx:///Data/database.sqlite"
.
由于该文件是只读的,因此您无法对其进行写入。如果您需要这样做,您必须首先将其复制到可写位置(例如ApplicationData.Current.LocalFolder
):
// get reference to read-only copy
StorageFile readonlyDb = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///Data/database.sqlite"));
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
// make a copy in a writeable location
StorageFile DataFile = await readonlyDb.CopyAsync(localFolder, "database.sqlite");
// now open a sqlite connection to DataFile...