1

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.

Community
  • 1
  • 1
Ajay
  • 25
  • 1
  • 6
  • Which version of System.Data.SQLite are you using? – CL. Jan 13 '16 at 08: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. – Ajay Jan 13 '16 at 08:57
  • 3.8.11.1 is the version of the SQLite library itself. What is the version of the C# System.Data.SQLite package? – CL. Jan 13 '16 at 09: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. – Ajay Jan 13 '16 at 09:13

4 Answers4

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);
      }
}
Ramgy Borja
  • 2,330
  • 2
  • 19
  • 40
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();
                    }
DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
Hicham
  • 11
  • 2
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.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • Okay I am going to try it will come back soon thanks in advance! – Ajay Jan 13 '16 at 09:37
  • I upgraded System.Data.Sqlite from version 1.0.74.0 to 1.0.99.0 now BackupDatabase() method is working thanks a lot. – Ajay Jan 13 '16 at 11:35
0

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();
    }
}