backuprestore.aspx.cs

来自「SQL+Asp.net(c#)网上考试系统」· CS 代码 · 共 108 行

CS
108
字号
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class HouAdmin_BackupRestore : System.Web.UI.Page
{
    string strCon = "Data Source=(local);DataBase=db_Examination;User ID=sa;PWD=sa";
    //SqlConnection sqlCon = new SqlConnection(strCon);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 创建连接及执行数据库操作
            string db_query = "sp_helpdb";

            SqlCommand myCommand = new SqlCommand(db_query, new SqlConnection(strCon));
            myCommand.Connection.Open();
            SqlDataReader dr = myCommand.ExecuteReader();

            // 将数据库列表绑定到下拉列表控件(DropDownList)
            dbDropDownList.DataSource = dr;
            dbDropDownList.DataTextField = "name";
            dbDropDownList.DataBind();

            //关闭DataReader对象和数据库连接
            dr.Close();
            myCommand.Connection.Close();
        }
    }

    protected void dbDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        pathTextBox.Text = @"d:\BACKUP\" + dbDropDownList.SelectedValue + ".bak";
    }

    protected void backupButton_Click(object sender, System.EventArgs e)
    {
        string path = pathTextBox.Text;
        string dbname = dbDropDownList.SelectedValue;

        string backupSql = "use master;";
        backupSql += "backup database @db_Examination to disk = @path;";

        SqlCommand myCommand = new SqlCommand(backupSql, new SqlConnection(strCon));

        myCommand.Parameters.Add("@db_Examination", SqlDbType.Char);
        myCommand.Parameters["@db_Examination"].Value = dbname;
        myCommand.Parameters.Add("@path", SqlDbType.Char);
        myCommand.Parameters["@path"].Value = path;

        try
        {
            myCommand.Connection.Open();
            myCommand.ExecuteNonQuery();           
            Response.Write("<script lanuage=javascript>alert('恭喜你,备份成功!');location='javascript:history.go(-1)'</script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script lanuage=javascript>alert('Sorry!备份失败!');location='javascript:history.go(-1)'</script>");           
        }
        finally
        {
            myCommand.Connection.Close();
        }
    }

    protected void restoreButton_Click(object sender, System.EventArgs e)
    {
        string path = pathTextBox.Text;
        string dbname = dbDropDownList.SelectedValue;

        string restoreSql = "use master;";
        restoreSql += "restore database @db_Examination from disk = @path;";

        SqlCommand myCommand = new SqlCommand(restoreSql, new SqlConnection(strCon));

        myCommand.Parameters.Add("@db_Examination", SqlDbType.Char);
        myCommand.Parameters["@db_Examination"].Value = dbname;
        myCommand.Parameters.Add("@path", SqlDbType.Char);
        myCommand.Parameters["@path"].Value = path;

        try
        {
            myCommand.Connection.Open();
            myCommand.ExecuteNonQuery();
            Response.Write("<script lanuage=javascript>alert('恭喜你,还原成功!');location='javascript:history.go(-1)'</script>");
        }
        catch (Exception ex)
        {

            Response.Write("<script lanuage=javascript>alert('Sorry!还原失败!');location='javascript:history.go(-1)'</script>");
        }
        finally
        {
            myCommand.Connection.Close();
        }
    }    

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?