📄 default.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
{
//自定义GetDataSet()主要用来实例数据的绑定
public void GetDataSet()
{
SqlConnection con = new SqlConnection("server=.;pwd=;uid=sa;Database=master");
SqlDataAdapter da = new SqlDataAdapter("select name from sysdatabases", con);
DataSet myds = new DataSet();
con.Open();
da.Fill(myds, "x");
con.Close();
this.dropSqlName.DataSource = myds;
this.dropSqlName.DataTextField = "name";
this.dropSqlName.DataValueField = "name";
this.dropSqlName.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetDataSet();
}
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)
{
//定义分离数据表的T-SQL命令的字符串
string cmdtxt = "sp_detach_db @dbname='"+this.dropSqlName.SelectedValue+"'";
SqlConnection con = new SqlConnection("server=.;pwd=;uid=sa;Database=master");
try
{
con.Open();
SqlCommand com = new SqlCommand(cmdtxt, con);
com.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('分离成功!')</script>");
GetDataSet();//调用GetDataSet()方法重新来绑定数据
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
protected void btnRestore_Click(object sender, EventArgs e)
{
//获取上传文件的路径
string path1 = this.FileUpload1.PostedFile.FileName;
string path2 = this.fileShow.PostedFile.FileName;
string cmdtxt1 = "server=(local);Database=master;uid=sa;pwd=;";
//定义附加数据库的T-SQL命令的字符串
string cmdtxt2 = "sp_attach_db @dbname='"+this.TextBox1.Text+"',@filename1='"+path1+"',@filename2='"+path2+"'";
SqlConnection mycon = new SqlConnection(cmdtxt1);
mycon.Open();
try
{
SqlCommand mycom = new SqlCommand(cmdtxt2, mycon);
mycom.ExecuteNonQuery();
mycon.Close();
Response.Write("<script>alert('附加成功!')</script>");
GetDataSet();//调用GetDataSet()方法重新来绑定数据
}
catch (Exception ex)
{
//捕获异常信息
Response.Write(ex.Message.ToString());
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedValue == "分离数据库")
{
//调用GetDataSet()方法重新来绑定数据
GetDataSet();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -