-2

I am following the guidance provided here:

Sqlite Online Backup Using System.Data.Sqlite

And it's working as I expect, but I'd like to pass the BackupDb.db file into a SaveFileDialog so that user can save the file outside of the applications folder, preferably on an external drive. I just can't figure this out.

This is what I have so far:

private void btn_backup_db_Click(object sender, RoutedEventArgs e)
    {
        var ofd1 = new Microsoft.Win32.SaveFileDialog();
        using (var source = new SQLiteConnection("Data Source=database.db; Version=3;"))
        using (var destination = new SQLiteConnection("Data Source=BackupDb.db; Version=3;"))
        {
            source.Open();
            destination.Open();
            source.BackupDatabase(destination, "main", "main", -1, null, 0);
            //Microsoft.Win32.SaveFileDialog saveFileDialog;
            ofd1.ShowDialog();
        }

I know I am missing a step, but I'm not sure what step. I have the ofd1 variable defined as opening a SaveFileDialog, and I'm telling it to open with of1.ShowDialog(); but I can't pass "main" or "destination" into the SaveFileDialog because SQLite doesn't have a definition for showing a dialog.

I'm trying to get the path to the file using:

        private string MyDirectory()
   {
        return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
   }

But I'm getting an exception stating that I don't have read access to the directory.

So then, I switched to trying this, but I'm not sure how to use it correctly. It's pointing to the base directory, but can't locate anything in bin\debug:

var dir = AppDomain.CurrentDomain.BaseDirectory;

This ended up being the answer, I included everything from btn_backup_db_Click:

   private void btn_backup_db_Click(object sender, RoutedEventArgs e)
    {

        var ofd1 = new Microsoft.Win32.SaveFileDialog();
        ofd1.Filter = "Database Files (*.db)|*.db";
        ofd1.FileName = "database";
            // customize file dialog properties here

            if (ofd1.ShowDialog() == true)
            {

            var path = Path.GetFullPath(ofd1.FileName);
            var destinationCnx = "Data Source=" + path + "; Version=3;";
            using (var source = new SQLiteConnection("Data Source=database.db; Version=3;"))
            using (var destination = new SQLiteConnection(destinationCnx))
            {
                source.Open();
                destination.Open();
                source.BackupDatabase(destination, "main", "main", -1, null, 0);
            }
            }
        else
        {
            MessageBox.Show("Error");
        }
    }

Thanks so much everyone!

7
  • 1
    What have you tried? What problems did you run into? Commented Sep 15, 2018 at 19:24
  • Call the SaveFileDialog, get the selected path. And then create the backup SQLiteConnection connectionstring from the previous step.. Commented Sep 15, 2018 at 19:26
  • @FrankerZ Sorry, updated the post. Commented Sep 15, 2018 at 20:37
  • @ShamusLocke You're not even doing anything with ofd1. After you show the dialog, why not get the location of said file, and DO SOMETHING with it? Commented Sep 15, 2018 at 20:38
  • @FrankerZ ' private string MyDirectory() { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }' I'm trying to locate the file with this, but I'm getting an exception saying I don't have read access. Commented Sep 15, 2018 at 20:41

1 Answer 1

0

The SaveFileDialog class is only an utility to let the user to select where he want to save a file (in the form of path).

To summarize, you need to display the file dialog before doing anything with sqlite. When you got the path, you need to build the back connectionstring.

var ofd1 = new SaveFileDialog();
// customize file dialog properties here

if (ofd1.ShowDialog() == true)
{
    var path = Path.GetFullPath(ofd1.FileName);
    var destinationCnx = "Data Source=" + path + "; Version=3;"

    // do sqlite stuff here
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm still not actually getting a file with this, when I put my SQLite code inside the if/then like that I'm getting "empty statement" and invalid expression term
You will not get a file, only a file path. You don't need more
Did you replace SQLiteConnection("Data Source=BackupDb.db; Version=3;") by SQLiteConnection(destinationCnx) ?
Wow, thanks. This worked. I'm going to update my question with your answer. I appreciate this. Can you explain in your answer why this method works and why the other two ways I tried didn't work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.