asp.net 还原、备份数据库

来源:互联网 发布:马克斯cms官网 编辑:程序博客网 时间:2024/06/04 18:22

 

             

Index.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
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 cmdtxt1 = "Server=(local);DataBase=master;Uid=sa;Pwd=";
            string cmdtxt2 = "Exec sp_helpdb";
            SqlConnection Con = new SqlConnection(cmdtxt1);
            Con.Open();
            SqlCommand mycommand = new SqlCommand(cmdtxt2, Con);
            SqlDataReader dr = mycommand.ExecuteReader();
            this.dropSqlName.DataSource = dr;
            this.dropSqlName.DataTextField = "name";
            this.dropSqlName.DataBind();
            dr.Close();
            Con.Close();
        }
     }
    protected void btnBackup_Click(object sender, EventArgs e)
    {
        string cmdtxt1 = "Server=(local);database='"+this.dropSqlName.SelectedValue+"';Uid=sa;Pwd=";
        string cmdtxt2 = "backup database "+this.dropSqlName.SelectedValue+" to disk='"+this.TextBox1.Text.Trim()+".bak'";
        SqlConnection Con = new SqlConnection(cmdtxt1);
        Con.Open();
        try
        {
            if(File.Exists(this.TextBox1.Text.Trim()))
            {
                Response.Write("<script language=javascript>alert('此文件已存在,请从新输入!');location='Index.aspx'</script>");
                return;
            }
            SqlCommand Com = new SqlCommand(cmdtxt2, Con);
            Com.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('备份数据成功!');location='Index.aspx'</script>");
        }
        catch (Exception ms)
        {
            Response.Write(ms.Message);
            Response.Write("<script language=javascript>alert('备份数据失败!')</script>");
        }
        finally
        {
            Con.Close();
        }
    }
    protected void btnRestore_Click(object sender, EventArgs e)
    {
        string path = this.fileShow.PostedFile.FileName; //获得备份路径及数据库名称
        string dbname = this.dropSqlName.SelectedValue;
        string cmdtxt1 = "Server=(local);database='" + this.dropSqlName.SelectedValue + "';Uid=sa;Pwd=";
        string cmdtxt2 = "use master restore database "+dbname+" from disk='"+path+"'";
        SqlConnection Con = new SqlConnection(cmdtxt1);
        Con.Open();
        try
        {
            SqlCommand Com = new SqlCommand(cmdtxt2, Con);
            Com.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('还原数据成功!');location='Index.aspx'</script>");
        }
        catch (Exception ms)
        {
            Response.Write(ms.Message);
            Response.Write("<script language=javascript>alert('还原数据失败!')</script>");
        }
        finally
        {
            Con.Close();
        }
    }
}