要对xxx.mdb这个数据库文件进行数据备份然后进行数据恢复应该怎么实现??

解决方案 »

  1.   

    #region 备份数据库
    public bool Backupdb(string FilePath)
    {
    string rLink;
    string sFile = Application.StartupPath + @"\sdms.mdb";
    string tFile = FilePath;
    if(!File.Exists(sFile))
    {
    rLink = "无法找到数据库文件!\n"+sFile;
    MessageBox.Show (rLink,"错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
    return false;
    }
    if(File.Exists (tFile))
    {
    string msg;
    msg = "目标路径已经存在同名文件,是否覆盖?\n";
    DateTime ct = File.GetCreationTime (tFile);
    DateTime lw = File.GetLastWriteTime (tFile);
    msg += "文件名:"+tFile+"\n";
    msg += "建立时间:"+ct.ToString ()+"\n";
    msg += "最后写入时间:"+lw.ToString ();
    if(MessageBox.Show (msg,"警告",MessageBoxButtons.YesNo,MessageBoxIcon.Question)!=DialogResult.Yes)
    {
    rLink = "已取消";
    return false;
    }
    }
    try
    {
    File.Copy (sFile,tFile,true);
    rLink = "备份成功!";
    }
    catch(FileNotFoundException e)
    {
    MessageBox.Show (e.Message,"错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
    rLink = "备份数据库失败!";
    return false;
    }
    catch(DirectoryNotFoundException e)
    {
    MessageBox.Show (e.Message,"错误",MessageBoxButtons.OK ,MessageBoxIcon.Error  );
    rLink = "备份数据库失败!";
    return false;
    }
    catch(IOException e)
    {
    MessageBox.Show (e.Message,"错误",MessageBoxButtons.OK ,MessageBoxIcon.Error  );
    rLink = "备份数据库失败!";
    return false;
    }
    return true;
    }
    #endregion
      

  2.   

    数据库恢复不就是文件的copy吗?首先添加引用:using System.IO ;
    using JRO;//压缩mdb用的然后: #region 压缩并备份源数据库
    public void CompactBackUpDB(string FromDB,string ToDB)
    {
    try 

    JRO.JetEngine jro; 
    jro = new JRO.JetEngine(); 
    if(System.IO .File .Exists (ToDB))
    {
                         File.Delete(ToDB); 
    }
     jro.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FromDB+";","Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ToDB + ";"); 
    File.Delete  (FromDB);
    File.Copy(ToDB, FromDB, true); 



    catch (System.Exception CompactAccessDB_Err) 

    throw CompactAccessDB_Err; 

    }
    #endregion #region 退出时压缩并备份数据库到指定位置
    public void ExitEvent()
    {
    try
    {
    string FromDB=Application.StartupPath +"..\\sdms.mdb";
    string ToDB=Application.StartupPath +"..\\BackUp\\sdmsbak.mdb";
    if(File.Exists (ToDB))
    {
    File.Delete (ToDB);
    }
    this.CompactBackUpDB (FromDB,ToDB);
    }
    catch(Exception err)
    {
    throw err;
    }
    finally
    {
    Application.Exit ();
    }
    }
    #endregion
      

  3.   

    因为你是要备份和恢复access数据库,而access是文件型数据库,所以直接拷贝和覆盖就可以了。
    所以也就是.net中流的操作,对文件的拷贝而已。