系统管理程序中如何备份数据库、还原数据库[代码]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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>

系统管理程序中如何备份数据库、还原数据库[代码],古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。