Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone!
I am writing a small program in c#.net ,when it comes to back up and restore .mdb file, I just could write the backup code as below :
C#
string CurrentDatabasePath = Environment.CurrentDirectory + @"\gongqin.mdb";
           string test = DateTime.Now.Year.ToString() + "Year" + "-" + DateTime.Now.Month.ToString() + "Month" + "-" + DateTime.Now.Day.ToString() + "Day";
           FolderBrowserDialog fbd = new FolderBrowserDialog();
           if (fbd.ShowDialog() == DialogResult.OK)
           {
               string PathtobackUp = fbd.SelectedPath.ToString();
               File.Copy(CurrentDatabasePath, PathtobackUp + @"\"+test+"BackUp.mdb", true);
               MessageBox.Show("successfully Backup! ");
           }


but I could not write the restore code, could anyone help me?
Posted

C#
private void Backup()
{
    string dbFileName =  "gongqin.mdb";
    string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory , dbFileName);
    string backTimeStamp = Path.GetFileNameWithoutExtension(dbFileName) + "_" + DateTime.Now.Year.ToString("yyyy-MM-dd") + Path.GetExtension(dbFileName);
    string destFileName = backTimeStamp + dbFileName;
    FolderBrowserDialog fbd = new FolderBrowserDialog();
    if (fbd.ShowDialog() == DialogResult.OK)
    {
        string PathtobackUp = fbd.SelectedPath.ToString();
        destFileName = Path.Combine(PathtobackUp, destFileName);
        File.Copy(CurrentDatabasePath,destFileName,  true);
        MessageBox.Show("successful Backup! ");
    }
}

private void Restore()
{
    string dbFileName = "gongqin.mdb";
    string pathBackup = @"C:\SomeFolder\Backup\gongqin_20120906.mdb"; //you may use file dialog to select this backuppath
    string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory, dbFileName);
    File.Copy(pathBackup, CurrentDatabasePath, true);
    MessageBox.Show("successful Restore! ");

}
 
Share this answer
 
 
Share this answer
 
Comments
Kuthuparakkal 6-Sep-12 3:49am    
irrelevant stuff
Member 10358986 28-Nov-13 1:29am    
Hello sir I have a same problem. I am using the above codes. but in both the above codes an error is there that is
"The given path's format is not supported."
Rickin Kane 6-Sep-12 6:25am    
buddy , think before you write a code , what if user dont have rights to copy the backup file , what if database is in use , do u think ur code can work , My god , programmer like u also exists in the world .
Kuthuparakkal 6-Sep-12 6:31am    
Criticisin is good! But remember to confirm whether you are right or wrong. Developer asked question on how to backup and restore Access Database, which is File System Database. And you posted how to do "Sql Server 2005" backup. Dont you have shame to post such a ridiculous answer. Or dont have the patience to read question ? I never saw a fool like you in my life before. Pity you exist to post nonsense all the time. Acquire some knowledge before you vomit emotions.
muhtarjanarkin 7-Sep-12 9:13am    
yeah, you are right ......

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