access数据库备份与恢复

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using ADOX;
using JRO;
using System.Text.RegularExpressions;

public partial class BackupRecover : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnBackup_Click(object sender, EventArgs e)
{
string sourcePath = urlconvertorlocal("/App_Data/HDZS.mdb");
string backupPath = urlconvertorlocal("/Copy/");
string message = Backup(sourcePath, backupPath);
WebUI.Alert(message, this.Page);
}
///


/// 备份Access数据库
///

/// 要备份的数据库绝对路径
/// 备份到的数据库绝对路径
///
public string Backup(string sourcePath, string backupPath)
{
if (!File.Exists(sourcePath))
{
return "源数据库不存在!";
}
try
{
string temp = DateTime.Now.Year.ToString();
temp += DateTime.Now.Month.ToString();
temp += DateTime.Now.Day.ToString();
temp += DateTime.Now.Hour.ToString();
temp += DateTime.Now.Minute.ToString();
temp += DateTime.Now.Second.ToString() + ".mdb";
backupPath = backupPath + temp;
File.Copy(sourcePath, backupPath, true);
return "备份成功!";
}
catch (IOException ixp)
{
throw new Exception(ixp.ToString());
}
}

protected void btnRecover_Click(object sender, EventArgs e)
{
string backupPath = urlconvertorlocal("/Copy/") + this.ddlAccess.SelectedItem.Text.ToString();
string sourcePath = urlconvertorlocal("/App_Data/HDZS.mdb");
string message = Recover(backupPath, sourcePath);
WebUI.Alert(message, this.Page);
}

///
/// 恢复Access数据库
///

/// 备份数据库绝对路径
/// 当前数据库绝对路径
public string Recover(string backupPath, string sourcePath)
{
if (!File.Exists(backupPath))
{
return "备份数据库不存在!";
}
try
{
File.Copy(backupPath, sourcePath, true);
return "恢复成功!";
}
catch (IOException ixp)
{
throw new Exception(ixp.ToString());
}

}


/////根据指定的文件名称创建ACCESS数据库
/////mdbPath:要创件的ACCESS绝对路径
//public void Create(string mdbPath)
//{

// if (File.Exists(mdbPath)) //检查数据库是否已存在
// {
// return "目标数据库已存在!";
// }
// // 可以加上密码,这样创建后的数据库必须输入密码后才能打开
// mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=App_Data/" + mdbPath + ".mdb";
// // 创建一个CatalogClass对象的实例
// ADOX.CatalogClass cat = new ADOX.CatalogClass();
// // 使用CatalogClass对象的Create方法创建ACCESS数据库
// cat.Create(mdbPath);
// return "新建成功!";
//}
}

///


/// 转换为绝对路径
///

///
///
private string urlconvertorlocal(string imagesurl1)
{

string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录

string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"/"); //转换成绝对路径

return imagesurl2;

}

再添上两段。。
///
/// 获得文件夹内文件名
///

///
///
protected void btnSearch_Click(object sender, EventArgs e)
{
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("Copy/"));

System.IO.FileInfo[] aa = info.GetFiles();

for (int i = 0; i < aa.Length; i++)
{
Regex re = new Regex(@"/w*/./w*");
string st = re.Match(aa[i].FullName).Value.ToString();
int num = i;
this.DropDownList1.Items.Insert(i, new ListItem(st, i.ToString()));
}
}

///
/// 删除指定文件
///

///
///
protected void btnDel_Click(object sender, EventArgs e)
{

System.IO.FileInfo file = new System.IO.FileInfo(urlconvertorlocal("/Copy/") + this.ddlAccess.SelectedItem.Text.ToString());
if (!file.Exists)
{
WebUI.Alert("文件不存在!", this.Page);
}
else
{
file.Delete();
WebUI.AlertAndRedirect("删除成功!", "Edit_Access.aspx", this.Page);
}
}

相关文档
最新文档