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

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>

系统管理程序中如何备份数据库、还原数据库[代码],布布扣,bubuko.com

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

上一篇:mssqlserver,mysql,oracle分页查询


下一篇:mysqldump导入导出数据