⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.aspx.cs

📁 这是asp.net^和Visual C++Sharp编写的串并口通讯的书籍 源代码
💻 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();
        }
        if (this.RadioButtonList1.SelectedIndex == 1)
        {
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            this.Panel3.Visible = false;
            this.Panel4.Visible = true;
        }
        else
        {
            this.Panel2.Visible = true;
            this.Panel1.Visible = false;
            this.Panel3.Visible = true;
            this.Panel4.Visible = false;
        }
    }
    protected void btnBackup_Click(object sender, EventArgs e)
    {
        string cmdtxt1 = "Server=(local);database='"+this.dropSqlName.SelectedValue+"';Uid=sa;Pwd=";
        //定义备数据库的T-SQL命令的字符串
        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 last = path.Substring(path.LastIndexOf(".") + 1);//获取文件的后缀名
        string dbname = this.dropSqlName.SelectedValue;
        if (last == "bak")   //判断是不是数据库备份文件
        {
            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();
            }
        }
        else
        {
            Response.Write("<script language=javascript>alert('必须是数据库文件!');</script>"); 
        }

    }
}

⌨️ 快捷键说明

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