zoukankan      html  css  js  c++  java
  • 如何使用ASP.NET备份和恢复SqlServer数据库

    首先我们先分析ASP备份SQL数据库的代码,详细如下:

    备份SqlServer数据库:

    string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';
    Uid=sa;Pwd=";
    string SqlStr2 = "backup database " + this.DropDownList1.SelectedValue + " to disk='" + this.TextBox1.Text.Trim() + ".bak'";
    SqlConnection con = new SqlConnection(SqlStr1); con.Open();
    try
    {
    if (File.Exists(this.TextBox1.Text.Trim()))
    {
    Response.Write("<script language=javascript>alert('此文件已存在,请从新输入!');
    location='Default.aspx'</script>");
    return;
    }
    SqlCommand com = new SqlCommand(SqlStr2, con);
    com.ExecuteNonQuery();
    Response.Write("<script language=javascript>alert('备份数据成功!');
    location='Default.aspx'</script>");
    }
    catch (Exception error)
    {
    Response.Write(error.Message);
    Response.Write("<script language=javascript>alert('备份数据失败!')</script>");

     }
    finally
    {
    con.Close();
    }

    下面是ASP.NET还原SQL数据库的方法,代码如下:

    还原SqlServer数据库:

    string path = this.FileUpload1.PostedFile.FileName;
    //获得备份路径及数据库名称 string dbname = this.DropDownList1.SelectedValue;
    string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';
    Uid=sa;Pwd="; string SqlStr2 = "use master restore database " + dbname + " from disk='" + path + "'";
    SqlConnection con = new SqlConnection(SqlStr1); con.Open();
    try
    {

     SqlCommand com = new SqlCommand(SqlStr2, con);
    com.ExecuteNonQuery();
    Response.Write("<script language=javascript>alert('还原数据成功!');
    location='Default.aspx'</script>");
    }
    catch (Exception error)
    {
    Response.Write(error.Message);
    Response.Write("<script language=javascript>alert('还原数据失败!')</script>");
    }
    finally
    {
    con.Close();

     }

  • 相关阅读:
    php安全编程&python测试实例编写
    MySQL注入技巧性研究
    第一届“百度杯”信息安全攻防总决赛
    不想在315“中奖”?你得躲过这些坑!
    这些故事你尽管听,不奇葩算我输!
    str2-045漏洞事件,你想要的这里都有
    python多线程在渗透测试中的应用
    【ZCTF】easy reverse 详解
    UVA
    用Thinphp发送电子邮件的方法
  • 原文地址:https://www.cnblogs.com/cksis/p/2417908.html
Copyright © 2011-2022 走看看