question

SmilingMoon avatar image
0 Votes"
SmilingMoon asked SmilingMoon commented

UWP Sqlite connection (Microsoft.Data.Sqlite.SqliteConnection), does it support absolute path other than application folder?

From uwp app, I'm trying to test DB connection from local folder other than default application folder, it throws "unable to open database" error.

SqliteConnection cnn = new SqliteConnection($"Filename={dbPath}");
cnn.Open();

I used dbPath like: "C:\Users\good\MyApp\Data\testdb.db".
App already has brows file system access.

I just want to know if it's prevented totally or I made mistake although it supports it.
I couldn't find any info related to this absolute path. All examples and explanation from MS docs are related to the default app folder.

Thank you,

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AryaDing-MSFT avatar image
0 Votes"
AryaDing-MSFT answered SmilingMoon commented

Hi,

Welcome to Microsoft Q& A!

UWP Sqlite connection does not support folders other than application folder and local app folder.

UWP apps are running in the sandbox and when you run them they are installed into it. They are not running in your source code bin folder of your project. In order to make your code up and running, you need to add your db file to your project Assets folder, then set Build Action of this file to Content. The good thing is that your file is now included to your Installed app folder. The bad thing is that it is read only. It cannot perform operations, such as inserting data. To overcome it you need to copy it to local app folder, as follows:

   private async void MainPage_Loaded(object sender, RoutedEventArgs e)
     {
         string targetDbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Database\\Test1.db");
         if (!File.Exists(targetDbPath)) 
         {
             var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
             using (var input = await installedLocation.OpenStreamForReadAsync("Assets\\Test1.db")) 
             {
                 using (var output = await Windows.Storage.ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("Database\\Test1.db", Windows.Storage.CreationCollisionOption.FailIfExists))
                 {
                     await input.CopyToAsync(output);
                 }
             }                
         }    
     }


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Got it. It's clear enough!!

0 Votes 0 ·