zoukankan      html  css  js  c++  java
  • ASP.NET备份还原数据库

    核心技术:
    using System.Data.SqlClient;
    using System.IO;
    string SqlStr1 = "Server=(local);DataBase=master;Uid=sa;Pwd=";
    string SqlStr2 = "Exec sp_helpdb";
    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'";

    1.前台

    <table>
    <tr>
    <td style=" 100px"><span style="font-size: 9pt">操 作 数 据 库</span></td>
    <td><asp:DropDownList ID="DropDownList1" runat="server" Font-Size="9pt" Width="124px"></asp:DropDownList></td>
    <td style=" 100px"></td>
    </tr>
    <tr>
    <td style=" 100px"><span style="font-size: 9pt">备份名称和位置</span></td>
    <td style=" 100px"><asp:TextBox ID="TextBox1" runat="server" Font-Size="9pt" Width="117px"></asp:TextBox></td>
    <td style=" 100px"><span style="font-size: 9pt; color: #ff3300">(如D:\beifen)</span></td>
    </tr>
    <tr>
    <td colspan="3"><asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="备份数据库" /></td>
    </tr>
    </table>

    2.后台

    using System.Data.SqlClient;
    using System.IO;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    string SqlStr1 = "Server=(local);DataBase=master;Uid=sa;Pwd=";
    string SqlStr2 = "Exec sp_helpdb";
    SqlConnection con = new SqlConnection(SqlStr1);
    con.Open();
    SqlCommand com = new SqlCommand(SqlStr2, con);
    SqlDataReader dr = com.ExecuteReader();
    this.DropDownList1.DataSource = dr;
    this.DropDownList1.DataTextField = "name";
    this.DropDownList1.DataBind();
    dr.Close();
    con.Close();
    }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    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();
    }
    }
    }

    还原SqlServer
    核心技术:
    string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
    string SqlStr2 = "use master restore database " + dbname + " from disk='" + path + "'";

    1.前台
    <table>
    <tr>
    <td style=" 100px; height: 21px"><span style="font-size: 9pt">操作 数 据 库</span></td>
    <td><asp:DropDownList ID="DropDownList1" runat="server" Font-Size="9pt" Width="124px"></asp:DropDownList></td>
    <td style=" 100px; height: 21px"></td>
    </tr>
    <tr>
    <td style=" 100px"><span style="font-size: 9pt">操 作 数 据 库</span></td>
    <td style=" 100px"><asp:FileUpload ID="FileUpload1" runat="server" Font-Size="9pt" Width="190px" /></td>
    <td style=" 100px">
    </td>
    </tr>
    <tr>
    <td colspan="3"><asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="还原数据库" /></td>
    </tr>
    </table>
    2.后台

    using System.Data.SqlClient;
    using System.IO;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    string SqlStr1 = "Server=(local);DataBase=master;Uid=sa;Pwd=";
    string SqlStr2 = "Exec sp_helpdb";
    SqlConnection con = new SqlConnection(SqlStr1);
    con.Open();
    SqlCommand com = new SqlCommand(SqlStr2, con);
    SqlDataReader dr = com.ExecuteReader();
    this.DropDownList1.DataSource = dr;
    this.DropDownList1.DataTextField = "name";
    this.DropDownList1.DataBind();
    dr.Close();
    con.Close();
    }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {


            string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称
            string dbname = this.DropDownList2.SelectedItem.Value;
            string SqlStr1 = "Server=.;database=master;Uid=sa;Pwd=";
            string SqlStr2 = "use master restore database " + dbname + " from disk='" + path + "'";
            string SqlStr3 = "exec killspid 'TW_ZLGD'";
            SqlConnection con = new SqlConnection(SqlStr1);
            con.Open();
            try
            {
                SqlCommand com2 = new SqlCommand(SqlStr3, con);
                com2.ExecuteNonQuery();
                SqlCommand com = new SqlCommand(SqlStr2, con);
                com.ExecuteNonQuery();
                MessageBox.Show(this, "还原数据成功!");
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "还原数据失败! 原因:"+error.Message);
            }
            finally
            {
                con.Close();
            }

    }

    需要在master数据库下建立个名字为"killspid"的存储过程

    CREATE proc killspid (@dbname varchar(20))     
    as     
    begin     
    declare @sql nvarchar(500)     
    declare @spid int    
    set @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id('''+@dbname+''')'     
    exec (@sql)     
    open getspid     
    fetch next from getspid into @spid     
    while @@fetch_status <>-1     
    begin     
    exec('kill '+@spid)     
    fetch next from getspid into @spid     
    end     
    close getspid     
    deallocate getspid     
    end
    GO

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yytt123622/archive/2009/01/14/3765641.aspx

  • 相关阅读:
    Linux程序调试GDB——数据查看
    linux C++ 共享库导出类
    MyBatis 通用Mapper接口 Example的实例
    mybatis3 @SelectProvider
    Maven命令行使用 mvn clean package
    Springboot IDEA eclipse 打包
    Intellij IDEA Debug
    SpringCloud之Eureka 服务注册和服务发现基础篇2
    SpringCloud组件和概念介绍1
    IntelliJ IDEA SVN
  • 原文地址:https://www.cnblogs.com/hateyoucode/p/1648281.html
Copyright © 2011-2022 走看看