namespace BLL { public class SystemManager { private DAL.SystemService service = new DAL.SystemService(); /// <summary> /// 备份数据库 /// </summary> /// <param name="path"></param> /// <returns></returns> public string DataSoureBack(string path,string oName) { string demo = service.DataSoureBack(path); MODEL.BackMsg model= new MODEL.BackMsg { BackPath=path, Operated=oName, }; if (string.IsNullOrEmpty(demo) || demo == "success") { model.OperatedResult = "成功"; model.ReasonsFailure = ""; } else { model.OperatedResult = "失败"; model.ReasonsFailure = demo; } new BLL.BackMsgManager().AddBackMsg(model); return demo; } /// <summary> /// 还原数据库 /// </summary> /// <param name="path"></param> /// <returns></returns> public string RestoreData(string path) { return service.RestoreData(path); } } }
namespace DAL { public class SystemService { public string DataSoureBack(string path) { string result = string.Empty; SqlConnection conn = new SqlConnection("Server=.;Database=master;User ID=sa;Password=Dhcs)_+;"); SqlCommand cmdBK = new SqlCommand(); cmdBK.CommandType = CommandType.Text; cmdBK.Connection = conn; cmdBK.CommandText = @"backup database lnsmB2B to disk='" + path + "' with init"; try { conn.Open(); cmdBK.ExecuteNonQuery(); result = "success"; } catch (Exception ex) { result = ex.Message; } finally { conn.Close(); conn.Dispose(); } return result; } public string RestoreData(string path) { string temp = string.Empty; SqlConnection conn = new SqlConnection("Server=.;Database=master;User ID=sa;Password=Dhcs)_+;Trusted_Connection=False"); conn.Open(); //KILL DataBase Process SqlCommand cmd = new SqlCommand("SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='lnsmB2B'", conn); SqlDataReader dr; dr = cmd.ExecuteReader(); ArrayList list = new ArrayList(); while (dr.Read()) { list.Add(dr.GetInt16(0)); } dr.Close(); for (int i = 0; i < list.Count; i++) { cmd = new SqlCommand(string.Format("KILL {0}", list[i]), conn); cmd.ExecuteNonQuery(); } SqlCommand cmdRT = new SqlCommand(); cmdRT.CommandType = CommandType.Text; cmdRT.Connection = conn; cmdRT.CommandText = @"restore database lnsmB2B from disk='" + path + "'"; try { cmdRT.ExecuteNonQuery(); temp = "success"; } catch (Exception ex) { temp = ex.Message; } finally { conn.Close(); } return temp; } } }
namespace BLL { public class BackMsgManager { private DAL.BackMsgService service = new DAL.BackMsgService(); /// <summary> /// 返回所有的数据备份记录 /// </summary> /// <returns></returns> public IList<MODEL.BackMsg> GetBackMsg() { return service.GetBackMsg(); } /// <summary> /// 添加一条新的数据备份记录 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool AddBackMsg(MODEL.BackMsg model) { return service.AddBackMsg(model); } /// <summary> /// 删除指定ID的数据备份记录 /// </summary> /// <param name="id"></param> /// <returns></returns> public bool DeleteBackMsg(string id) { return service.DeleteBackMsg(id); } } }
/// <summary>
/// 获取网站数据备份路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string DataSoureBackPath()
{
return HttpContext.Current.Server.MapPath("~/" + ConfigurationSettings.AppSettings["DataBackUrl"] + DateTime.Now.ToString("yyyyMMddhhmmss") + "lnsmB2B.bak");
}
然后再 web.config里配置保存的路径
<appSettings>
<!-- 数据库备份路径 -->
<add key="DataBackUrl" value="admin/dataBack/" />
</appSettings>