簡體   English   中英

如何使用 C# 或 Sqlite 查詢進行 Sqlite 數據庫備份

[英]How to take Sqlite database backup using C# or Sqlite query

內存流作為數據庫

我在這個鏈接上使用了代碼,它給了我錯誤信息

System.Data.Sqlite.SqliteConnection 中不存在 BackupDatabase()

是否有任何 Sqlite 查詢來備份或導出 Sqlite 數據庫。 然后再恢復

我想在一個系統上導出數據庫,然后將其導入另一個系統,以便在兩個系統上安裝相同的 Windows 應用程序。 我想在單擊按鈕時實施此活動。

Sqlite鏈接中的備份數據庫鏈接試試這個功能

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

試試這個

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

System.Data.SQLite 1.0.74.0 已經過時了。 對於備份 API 支持,至少需要 1.0.80.0 版本,但您也可以升級到當前版本。

SQLite 版本 3.27.0(2019-02-07)中引入的 VACUUM INTO 命令可以作為替代命令。 除了備份之外,它還重建數據庫文件,將其重新打包到最少的磁盤空間中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM