-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!

  • 1
    What have you tried? What problems did you run into? – Blue Sep 15 '18 at 19:24
  • Call the SaveFileDialog, get the selected path. And then create the backup SQLiteConnection connectionstring from the previous step.. – Kalten Sep 15 '18 at 19:26
  • @FrankerZ Sorry, updated the post. – Shamus Locke Sep 15 '18 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? – Blue Sep 15 '18 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. – Shamus Locke Sep 15 '18 at 20:41
  • So where is all this information in your question!? Show some research, help us help you. – Blue Sep 15 '18 at 20:47
  • @FrankerZ very sorry. I'll keep this in mind in the future. I wrote an earlier post that described all of this, following answers and comments but nothing worked as expected. – Shamus Locke Sep 15 '18 at 21:00

1 Answers1

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
}
Kalten
  • 4,092
  • 23
  • 32
  • 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 – Shamus Locke Sep 15 '18 at 21:46
  • You will not get a file, only a file path. You don't need more – Kalten Sep 15 '18 at 21:48
  • Did you replace `SQLiteConnection("Data Source=BackupDb.db; Version=3;")` by `SQLiteConnection(destinationCnx)` ? – Kalten Sep 15 '18 at 21:52
  • 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? – Shamus Locke Sep 15 '18 at 22:07