pavol.franek
pavol.franek

Reputation: 1416

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? I add my file called database.sqlite to my app like new item. 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.

Upvotes: 0

Views: 550

Answers (1)

chue x
chue x

Reputation: 18803

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. 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".

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 (e.g. 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...

Upvotes: 1

Related Questions