//数据备份
bool bl=false;
string adr=AppDomain.CurrentDomain .BaseDirectory + "database.mdb";
SaveFileDialog sfd=new SaveFileDialog ();
sfd.Filter = "所有文件 | *";
sfd.DefaultExt = ".mdb";
if(sfd.ShowDialog () == DialogResult.OK )
{
bl=true;
}
if(bl==true)
{
try
{
File.Copy (adr,sfd.FileName ,true);
FileStream fs=new FileStream (AppDomain.CurrentDomain .BaseDirectory + "back.txt" ,FileMode.Create ,FileAccess.Write );
BinaryFormatter b=new BinaryFormatter();
b.Serialize(fs,sfd.FileName);
fs.Close ();
MessageBox.Show ("备份成功!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information );
}
catch
{
MessageBox.Show ("操作错误,请重新备份!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Error);
}
}
//数据还原
FileStream fs1=new FileStream (AppDomain.CurrentDomain .BaseDirectory + "back.txt",FileMode.Open ,FileAccess.Read );
BinaryFormatter b1=new BinaryFormatter ();
File.Copy ((b1.Deserialize (fs1)).ToString (),AppDomain.CurrentDomain .BaseDirectory + "database.mdb" ,true);;
fs1.Close ();
MessageBox.Show ("还原成功!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information );
//所需添加的命名空间
using System.Data.OleDb;
using System.IO ;
using System.Runtime .Serialization ;
using System.Runtime .Serialization .Formatters .Binary ;
我已经测试过了,应该没有问题的,有点功底的就应该可以看懂的