ASP.NET中数据库备份的源码

using System;

using System.Data.SqlClient;

using System.Web.UI;


public partial class BackupDatabase : Page

{

   protected void Page_Load(object sender, EventArgs e)

   {

       if (!IsPostBack)

       {

           if (Session["UserName"] == null && Session["Pwd"] == null)

           {

               Response.Redirect("Prompt.aspx");

           }

           else

           {

               LblMessage.Text = "您好! " + Session["UserName"] + " 欢迎您登录本网站!";

               try

               {

                   BindDropDownList();

               }

               catch

               {

                   BindDropDownList();

               }

           }

       }

   }


   public void BindDropDownList()

   {

       string cmdtxt1 = "Data Source=localhost;Initial Catalog=master;Integrated Security=True";

       string cmdtxt2 = "Exec sp_helpdb";

       SqlConnection connection = new SqlConnection(cmdtxt1);

       connection.Open();

       SqlCommand command = new SqlCommand(cmdtxt2, connection);

       SqlDataReader dr = command.ExecuteReader();

       DDLDataBase.DataSource = dr;

       DDLDataBase.DataTextField = "name";

       DDLDataBase.DataBind();

       dr.Close();

       connection.Close();

   }



   protected void ImgBtnOK_Click(object sender, ImageClickEventArgs e)

   {

       string cmdtxt1 = "Data Source=localhost;Initial Catalog=‘" + DDLDataBase.SelectedValue +

                        "‘;Integrated Security=True";

       string cmdtxt2 = "backup database " + DDLDataBase.SelectedValue + " to disk=‘" + TextBox1.Text.Trim() + ".bak‘";

       SqlConnection connection = new SqlConnection(cmdtxt1);

       connection.Open();

       try

       {

           SqlCommand command = new SqlCommand(cmdtxt2, connection);

           command.ExecuteNonQuery();

           Response.Write("<script language=javascript>alert(‘备份数据成功!‘);location=‘javascript:history.go(-1)‘</script>");

       }

       catch (Exception ms)

       {

           Response.Write(ms.Message);

           Response.Write("<script language=javascript>alert(‘备份数据失败!‘);location=‘javascript:history.go(-1)‘</script>");

       }

       finally

       {

           connection.Close();

       }

   }


本文出自 “黄忠” 博客,请务必保留此出处http://tyjhz.blog.51cto.com/8756882/1395565

ASP.NET中数据库备份的源码,布布扣,bubuko.com

ASP.NET中数据库备份的源码

上一篇:成长笔记 - mysql-5.5.25-winx64安装步骤


下一篇:Hibernate MySQL 中文乱码问题