C# 用c语言恢复sqlite数据库#
我编写了一个c#应用程序,可以在任何其他计算机上工作,因此我使用了sqlite数据库。我想在此应用程序中备份和还原数据。对于备份,它是可以的。我使用以下代码:C# 用c语言恢复sqlite数据库#,c#,sqlite,restore,C#,Sqlite,Restore,我编写了一个c#应用程序,可以在任何其他计算机上工作,因此我使用了sqlite数据库。我想在此应用程序中备份和还原数据。对于备份,它是可以的。我使用以下代码: private void button1_Click(object sender, EventArgs e) { using (var source = new SQLiteConnection("Data Source=bazarganidb.db;version=3")) using (var destinat
private void button1_Click(object sender, EventArgs e)
{
using (var source = new SQLiteConnection("Data
Source=bazarganidb.db;version=3"))
using (var destination = new SQLiteConnection("Data Source=" + textBox1.Text + "/" + DateTime.Now.ToString("yyyyMMdd") + "backup.db"))
{
source.Open();
destination.Open();
source.BackupDatabase(destination, "main", "main", -1, null, 0);
}
}
但是我不知道如何恢复。如何恢复备份的数据库?
我搜索了很多,但没有结果。试试这个
code
class Program
{
private static readonly string filePath = Environment.CurrentDirectory;
static void Main(string[] args)
{
var filename = "bazarganidb.db";
var bkupFilename = Path.GetFileNameWithoutExtension(filename) + ".bak";
CreateDB(filePath, filename);
BackupDB(filePath, filename, bkupFilename);
RestoreDB(filePath, bkupFilename, filename, true);
}
private static void RestoreDB(string filePath, string srcFilename, string
destFileName, bool IsCopy = false)
{
var srcfile = Path.Combine(filePath, srcFilename);
var destfile = Path.Combine(filePath, destFileName);
if (File.Exists(destfile)) File.Delete(destfile);
if (IsCopy)
BackupDB(filePath, srcFilename, destFileName);
else
File.Move(srcfile, destfile);
}
private static void BackupDB(string filePath, string srcFilename, string
destFileName)
{
var srcfile = Path.Combine(filePath, srcFilename);
var destfile = Path.Combine(filePath, destFileName);
if (File.Exists(destfile)) File.Delete(destfile);
File.Copy(srcfile, destfile);
}
private static void CreateDB(string filePath, string filename)
{
var fullfile = Path.Combine(filePath, filename);
if (File.Exists(fullfile)) File.Delete(fullfile);
File.WriteAllText(fullfile, "this is the dummy data");
}
}
对于恢复,只需使用以下内容:
string BackupPath = "Backup/Backup.db";
string restorePath = "Mydb.db";
File.Copy(BackupPath, restorePath, true);
//copy has three parameters : string SourceFileName,string DesFileName,bool Overwrite
由于您的关注,您只需复制/移动备份文件覆盖bazarganidb.dbs即可。但sqlite文件位于bin/Debug文件夹中。我可以删除它并将新备份复制到它吗?