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!