• 数据库备份与还原


    数据库远程备份与还原
    1、Web.config
     <connectionStrings>
        <add name="BeiFenConnectionString" connectionString="Data Source=192.168.1.201;Initial Catalog=SchoolDB;User ID=huang;Password=123456;"/>
        
        <add name="HuanYuanConnectionStringRestore" connectionString="Data Source=192.168.1.201;Initial Catalog=master;User ID=huang;Password=123456;"/>
      </connectionStrings>
     <appSettings>
      <!--数据库备份文件存放地址-->
      <add key="DBBack" value="E:databaseSchoolDB.bak" />
     </appSettings>
    2、aspx
    <asp:Button ID="Button1" runat="server" Text="备份" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="还原" OnClick="Button2_Click" />
    3、aspx.cs
     //数据库备份
            protected void Button1_Click(object sender, EventArgs e)
            {
                string con_str = ConfigurationManager.ConnectionStrings["BeiFenConnectionString"].ToString();
                SqlConnection conn = new SqlConnection(con_str);
                SqlCommand cmdBK = new SqlCommand();
                cmdBK.CommandType = CommandType.Text;
                cmdBK.Connection = conn;
                cmdBK.CommandText = @"backup database SchoolDB to disk='E:BackupSchoolDB" + ".bak'" + " with init";
                try
                {
                    conn.Open();
                    cmdBK.ExecuteNonQuery();
                    System.Web.HttpContext.Current.Response.Write("<Script>alert('数据库备份或更新备份成功!');</Script>");
                }
                catch (Exception)
                {
                    System.Web.HttpContext.Current.Response.Write("<Script>alert('抱歉,服务器连接失败!或者要备份的数据库不存在于服务器中,请先尝试新建或还原!');</Script>");
                }
                finally
                {
                    conn.Close();
                }
            }
     
      //还原数据库
            protected void Button2_Click(object sender, EventArgs e)
            {
                string con_str = ConfigurationManager.ConnectionStrings["HuanYuanConnectionStringRestore"].ToString();
                SqlConnection conn = new SqlConnection(con_str);
                try
                {
                    conn.Open();
                    SqlCommand cmdRT = new SqlCommand();
                    cmdRT.CommandType = CommandType.Text;
                    cmdRT.Connection = conn;
                    cmdRT.CommandText = "select * from sys.databases where name='SchoolDB'";
                    object isHave = cmdRT.ExecuteScalar();
                    if (isHave == null)//不存在
                    {
                        cmdRT.CommandText = @"restore database SchoolDB from disk= 'E:BackupSchoolTest20120808" + ".bak'";
                        cmdRT.ExecuteNonQuery();
                        System.Web.HttpContext.Current.Response.Write("<Script>alert('数据库还原成功!');</Script>");
                    }
                    else
                    {
                        System.Web.HttpContext.Current.Response.Write("<Script>alert('此数据库已存在!');</Script>");
                    }
                }
                catch (Exception)
                {
                    System.Web.HttpContext.Current.Response.Write("<Script>alert('抱歉,服务器连接失败!或者要还原的数据库备份不存在于服务器中,请先尝试备份;或者数据库目前运转正常而不必还原!');</Script>");
                }
                finally
                {
                    conn.Close();
                }
    ----------------------------------------------------------------------------------------------
    还原数据库方案二------------【直接覆盖同名数据库】
    protected void Button2_Click(object sender, EventArgs e)
            {
                string con_str = ConfigurationManager.ConnectionStrings["HuanYuanConnectionStringRestore"].ToString();
                SqlConnection conn = new SqlConnection(con_str);
                try
                {
                    conn.Open();
                    //杀死所有正在使用要还原数据的数据库进程
                    SqlCommand cmd = new SqlCommand("SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='aaa'", conn);
                    SqlDataReader dr;
                    dr = cmd.ExecuteReader();
                    ArrayList list = new ArrayList();
                    while (dr.Read())
                    {
                        list.Add(dr.GetInt16(0));
                    }
                    dr.Close();
                    for (int k = 0; k < list.Count; k++)
                    {
                        cmd = new SqlCommand(string.Format("KILL {0}", list[k]), conn);
                        cmd.ExecuteNonQuery();
                    }
    
                    SqlCommand cmdRT = new SqlCommand();
                    cmdRT.CommandType = CommandType.Text;
                    cmdRT.Connection = conn;
                    cmdRT.CommandText = @"restore database SchoolDB from disk= 'E:BackupSchoolTest20120808.bak' with replace ";//若存在 则直接覆盖
                    cmdRT.ExecuteNonQuery();
                    System.Web.HttpContext.Current.Response.Write("<Script>alert('数据库还原成功!');</Script>");
                }
                catch (Exception)
                {
                    System.Web.HttpContext.Current.Response.Write("<Script>alert('抱歉,服务器连接失败!或者要还原的数据库备份不存在于服务器中,请先尝试备份;或者数据库目前运转正常而不必还原!');</Script>");
                }
                finally
                {
                    conn.Close();
                }
            }
  • 相关阅读:
    layui学习
    网络安装Centos x64 6.10
    给没有连接因特网的centos使用yum安装其他软件。
    Java使用线程池
    记录一次查看后台是否在运行资源备份上报到华为云存储的过程
    潭州课堂python
    什么是动态规划?动态规划的意义是什么?
    南明区教师信息管理系统之审批流程设计思路
    连接慢的主要原因是DNS解析导致
    jfinal中,render的时候如何取到view根目录
  • 原文地址:https://www.cnblogs.com/914556495wxkj/p/3514792.html
Copyright © 2020-2023  润新知