关于数据备份和数据恢复问题,急,谢谢帮忙啊 要对xxx.mdb这个数据库文件进行数据备份然后进行数据恢复应该怎么实现?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #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 数据库恢复不就是文件的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 因为你是要备份和恢复access数据库,而access是文件型数据库,所以直接拷贝和覆盖就可以了。所以也就是.net中流的操作,对文件的拷贝而已。 dataGridView1显示Dataset的内容,用哪一句??? 在登陆页面当中,我既可以用用户名进行登陆也可以用邮箱进行登陆? 将对象持久化的问题 急等版主和高手!!!!一个让人头痛的SQL Server与datagrid问题 =====高手请进,他人帮顶,对象不能从 DBNull 转换为其他类型,三天不解其意????? 非牛人物和不懂得数据库的就不要进来了 关于c#读取sqlserver 存储过程游标的问题 求助,这个怎么不对? 怎么实现qq用xml记住密码后的读取 如何取得客户端的控件值? 求救! 如何让ListBox自动滚动到指定的行? 连接建立SQL连接的问题
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
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
所以也就是.net中流的操作,对文件的拷贝而已。