27

How can a sqlite database be backed up in native C# code while the database is still online? All of the online backup api examples are in C code.

1 Answer 1

48

The online backup API was added to System.Data.Sqlite in version 1.0.80.0 - April 1, 2012. You can create a database backup while there are other external connections like so

using(var source = new SQLiteConnection("Data Source=ActiveDb.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);
}

Also, BackupDb.db will be created if it doesn't already exist.

Sign up to request clarification or add additional context in comments.

2 Comments

Your example is right, but I just want to remark that the transactions of both databases must be closed, otherwise you'll get a "unknown error" by sqLite!
I believe the "using" keyword takes care of closing the connections automatically.

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.