3

Memory Stream as DB

I have used code on this link which gave me errormessage that

BackupDatabase() does not exists in System.Data.Sqlite.SqliteConnection

Is there any Sqlite query to backup or export Sqlite database. And then restore it again

I want to export database on one system and then import it in another system for same windows application installed on both systems. I want to implement this activities on button click.

4
  • Which version of System.Data.SQLite are you using? Commented Jan 13, 2016 at 8:31
  • Version:- SQLite 3.8.11.1. I have added it in mozila firefox through Addons.I think it's providing only following features Tables,view,index and triggers.The work which I am thinking about is easier in SQL server but not in Sqlite Database. Commented Jan 13, 2016 at 8:57
  • 3.8.11.1 is the version of the SQLite library itself. What is the version of the C# System.Data.SQLite package? Commented Jan 13, 2016 at 9:06
  • There is one more thing when I was studying Sqlite Database Connection I found that extension to database name included in 'source' attribute in connection string were different, like '.s3db', '.db' etc at different places in searched results on Google. But the Database I have prepared is having extension '.sqlite' which is working properly with my windows form application. Commented Jan 13, 2016 at 9:13

4 Answers 4

4

backup database link in Sqlite link try this function

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);
      }
}
Sign up to request clarification or add additional context in comments.

Comments

3

The VACUUM INTO command introduced in SQLite version 3.27.0 (2019-02-07) can serve as an alternative. In addition to the backup it rebuilds the database file, repacking it into a minimal amount of disk space.

public void backup(string strDestination)
{
    using (var location = new SQLiteConnection(@"Data Source=C:\activeDb.db; Version=3;"))
    {
        SQLiteCommand sqlCmd = location.CreateCommand();
        sqlCmd.CommandText = $"VACUUM INTO '{strDestination}'";
        sqlCmd.ExecuteNonQuery();
    }
}

Comments

1

try this

using (var destination = new System.Data.SQLite.SQLiteConnection(string.Format("Data Source={0}\\DriversTrucksBackup.db; Version=3;", this.txtLocation.Text)))
                    {
                        da.cn.Open();
                        destination.Open();
                        da.cn.BackupDatabase(destination, "main", "main", -1, null, 0);
                        da.cn.Close();
                    }

Comments

0

System.Data.SQLite 1.0.74.0 is quite outdated. For the backup API support, at least version 1.0.80.0 would be required, but you could just as well upgrade to a current version.

2 Comments

Okay I am going to try it will come back soon thanks in advance!
I upgraded System.Data.Sqlite from version 1.0.74.0 to 1.0.99.0 now BackupDatabase() method is working thanks a lot.

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.