c# access数据库的备份和恢复?
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("数据库备份成功! ");
}
可是还原(数据库恢复)实现不到了,网上找不到代码了,哪位高手能帮忙? 展开
你也可以这样,我多写了个函数,做法如下
public bool Recover(string backPath,string dataPath)//还原数据库函数
{
if (!File.Exists(backPath))
{
throw new Exception("备份数据库不存在,无法还原");
}
try
{
File.Copy(backPath, dataPath, true);
}
catch (IOException ixp)
{
return false;
throw new Exception(ixp.ToString());
}
return true;
}
下面是点击按钮事件来选择要还原的路径
private void button9_Click(object sender, EventArgs e)//还原数据库路径选择
{
string path;
//用openFileDialog打开并确认需要备份的数据库文件
openFileDialog1.Filter = "数据库备份文件(*.mdb)|*.mdb";
openFileDialog1.FileName = "backup.mdb";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
path = openFileDialog1.FileName;
textBox2.Text = path;
}
}
下面按钮备份事件
private void button10_Click(object sender, EventArgs e)//装入数据库备份
{
string backPath = textBox2.Text;//从texeBox中获得备份文件
string dataPath = Application.StartupPath.ToString() + "\\METER.mdb";//还原后的数据库路径
if (Recover(backPath,dataPath))
{
MessageBox.Show("数据库还原成功");
}
else
{
MessageBox.Show("数据库还原失败");
}
}
2012-09-10