简体   繁体   English

在Windows应用商店中连接到SQL Lite数据库

[英]Connect to sql lite db in windows store app

Hi can someone recommend me how I can connect to sql lite db in Windows store app? 嗨,有人可以推荐我如何在Windows应用商店应用中连接到sql lite db吗? I add my file called database.sqlite to my app like new item. 我将名为database.sqlite的文件添加到我的应用程序中,就像新条目一样。 But I cannot find how I can connect to my existing file. 但是我找不到如何连接到现有文件的方法。 I find this: 我发现:

StorageFile DataFile = 
  await ApplicationData.Current.LocalFolder.GetFileAsync("database.sqlite");

But I dont know how write correct path to file. 但是我不知道如何写正确的文件路径。 Does anyone have some idea. 有谁有主意。

When you add a database file to your project, it is considered an asset of your project. 将数据库文件添加到项目时,该文件被视为项目的资产。 The file is stored with your application, and is in a read-only location. 该文件与您的应用程序一起存储,并且位于只读位置。

To access the read-only version, you have to use this: 要访问只读版本,您必须使用以下命令:

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
  new Uri("ms-appx:///relative/path/to/db/database.sqlite"));

Where relative/path/to/db is the path relative from the top level of your project. 其中relative/path/to/db是相对于项目顶层的相对路径。 For example if your project has a folder called Data that has your db file in it, then you would use "ms-appx:///Data/database.sqlite" . 例如,如果您的项目中有一个名为Data的文件夹,其中包含db文件,则应使用"ms-appx:///Data/database.sqlite"

Since the file is read-only, you cannot write to it. 由于该文件是只读文件,因此无法对其进行写入。 If you need to do so, you must first make a copy of it into a writeable location (eg ApplicationData.Current.LocalFolder ): 如果需要这样做,必须首先将其副本复制到可写位置(例如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...

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

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