c#备份和恢复访问数据库代码? [英] c# backup and restore access database code?

查看:100
本文介绍了c#备份和恢复访问数据库代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我正在用c#.net编写一个小程序,当涉及到备份和恢复.mdb文件时,我只需编写如下备份代码:

< pre lang =c#> string CurrentDatabasePath = Environment.CurrentDirectory + @ \gongqin.mdb;
string test = DateTime.Now.Year.ToString()+ 年份 + - + DateTime.Now.Month .ToString()+ + - + DateTime.Now.Day.ToString()+ ;
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog()== DialogResult.OK)
{
string PathtobackUp = fbd.SelectedPath.ToString();
File.Copy(CurrentDatabasePath,PathtobackUp + @ \ + test + BackUp.mdb true );
MessageBox.Show( 成功备份!);
}





但是我无法编写恢复代码,任何人都可以帮助我吗?

解决方案

  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( 成功备份!);
}
}

私人 void 恢复( )
{
string dbFileName = gongqin.mdb;
string pathBackup = @ C:\\ \\SomeFolder\Backup\gongqin_20120906.mdb; // 您可以使用文件对话框选择此backuppath
string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory,dbFileName);
File.Copy(pathBackup,CurrentDatabasePath, true );
MessageBox.Show( 成功恢复!);

}


请参阅下面的使用C#和.NET 2.0进行SQL Server 2005数据库备份和恢复

实现得非常好


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 :

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?

解决方案

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! ");

}


Refer to below SQL Server 2005 Database Backup and Restore using C# and .NET 2.0
implemented quite nicely


这篇关于c#备份和恢复访问数据库代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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