I created a piece of code to backup a SQLite db, but I keep getting an error and I can't figure it out. Any assistacne would be greatfully accepted.

C#
private void btnExport_Click(object sender, EventArgs e)
{
AC.cmd.Parameters.Clear();
var filePath = Application.StartupPath + "\\" + "Backup";
AC.OpenConnection();
var query = String.Format("BACKUP DATABASE [{0}] TO DISK='{1}'", AC.dbName, filePath);
AC.cmd.CommandType = CommandType.Text;
AC.cmd.CommandText = query;
AC.cmd.ExecuteNonQuery();
AC.CloseConnection();
}
Sam HobbsRank #107 · 20.5K reputationPosted May 10, 2024, 4:12 PM EDT
Are you sure you need the square brackets? Try:
Instead of:
Jaimin ShethiyaRank #177 · 13.7K reputationPosted May 11, 2024, 6:23 AM EDT
Hello,
Try below code.
public void backup(string strDestination) { using (var location = new SQLiteConnection(@"Data Source=C:\activeDb.db; Version=3;")) using (var destination = new SQLiteConnection(string.Format(@"Data Source={0}:\backupDb.db; Version=3;", strDestination))) { location.Open(); destination.Open(); location.BackupDatabase(destination, "main", "main", -1, null, 0); } }and also please refer for more info to below links.
https://blog.devgenius.io/backup-and-restore-sqlite-database-db-using-c-net-bbe25a428cdf
https://stackoverflow.com/questions/34760033/how-to-take-sqlite-database-backup-using-c-sharp-or-sqlite-query
Thanks
RICHARD KNOX100 reputationPosted May 11, 2024, 7:36 PM EDT
Jaimin,
The code works, but inside the backup file there is no tables or data. Its just a shell.
RICHARD KNOX100 reputationPosted May 11, 2024, 7:37 PM EDT
Sam,
I removed the brackets and it still doesnt accomplish anything.
Jaimin ShethiyaRank #177 · 13.7K reputationPosted May 12, 2024, 2:57 AM EDT
Hello,
Try below code.
string backupFilePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), @"\backup\backup.db");
string OriginalFile =@"D:\sqlite.db";
File.Copy(OriginalFile, backupFilePath, true);
Thanks