在C#中恢复sqlite数据库 [英] Restore sqlite database in C#

查看:144
本文介绍了在C#中恢复sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了可以在任何其他计算机上运行的ac#应用程序,所以我在sqlite数据库中使用。我想备份和恢复此应用程序中的数据。备份它没关系。我使用下面的代码:

I programmed a c# application that can work on any other computer so i used from sqlite database.I want to backup and restore data in this app.for backup it's ok.I use from below code:

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



但是我不知道restore.how我可以恢复数据库wichi backuped吗?我搜索了很多但没有结果。





我添加了以下代码来删除befor的sqlite文件并复制驱动器d中的新数据库。但是,当我点击按钮没有任何事情发生,没有错误或异常。但如果我将文件复制到另一个文件夹除了调试每个thinng是好的。但我的第一个数据库是在调试文件夹,它应该从这里更改




But i dont know about restore.how can i restore the database wichi backuped? I searche alot but no result.


I added bellow code to delete sqlite file of befor and copy new database that is in drive d.but when i click button nothing happen and no error or exception.but if i copy the file to another folder excep debug every thinng is ok.but my firs db is in debug folder and it should be change from here

try
            {
               string FileToReplace = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
 System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "bazarganidb.db");
                File.Delete(FileToReplace);
                string OriginalFile =@"D:\bazarganidb.db";
                
                
                //string FileToReplace = @"E:\BazarganiProj2\BazarganiProj\bin\Debug\bazarganidb.db";

                if(File.Exists(FileToReplace))
                File.Delete(FileToReplace);
                File.Copy(OriginalFile, FileToReplace,true);
               }
            catch (Exception k)
            {
                Console.WriteLine(k);
            }





我的尝试:



i搜索了很多但没有结果



What I have tried:

i searched alot but no result

推荐答案

SQLITE是一个基于单用户本地文件的数据库,不像基于多用户服务器的数据库系统,如MySql, Sql Server等...它的限制性要小得多。



因此,如果它是一个完整的备份/恢复,只需重命名为备份,并删除&重命名恢复。



更新:以下是如何操作的简单示例:

SQLITE is a single-user local file based DB unlike multi-user server based DB systems, like MySql, Sql Server, etc... it is far less restrictive.

So, if it is a complete backup/restore, simply rename to backup, and delete & rename to restore.

UPDATE: Here is a quick example of how to do it:
class Program
{
    private static readonly string filePath = Environment.CurrentDirectory;

    static void Main(string[] args)
    {
        var filename = "mock.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");
    }
}


这篇关于在C#中恢复sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆