using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

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);user id=myzf;pwd=123456;database=SQL_Data";
            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 + "';user id=myzf;pwd=123456";
        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();
        }
    }
}
