Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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);
    }
}

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


What I have tried:

i searched alot but no result
Posted
Updated 5-Nov-17 23:11pm
v4

1 solution

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:
C#
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");
    }
}
 
Share this answer
 
v3
Comments
Member 11727674 5-Nov-17 1:33am    
thanks beacuse of your attention.But the sqlite file is in bin/Debug folder. Can i delete it and copy new backup to it?
Graeme_Grant 5-Nov-17 2:10am    
Bin\Debug folder is running app folder. Yes, you can! :)
Member 11727674 6-Nov-17 4:48am    
thanks but i dont know why when i'm delete and copy the backup into debug folder nothing happen and its just for debug folder it hasn't any problem with another folders.can you help me about it?
Graeme_Grant 6-Nov-17 4:57am    
Update the question with the new code so that we can see.
Member 11727674 6-Nov-17 5:11am    
I updated it.regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900