Default.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>分离与附加数据库</title> </head> <body style="text-align: center"> <form id="form1" runat="server"> <div> <table cellpadding="0" cellspacing="0" style="width: 400px; height: 7px;"> <tr> <td style="height: 4px; text-align: left; width: 295px;"> </td> </tr> <tr> <td style="height: 18px; text-align: center; width: 295px;"> </td> </tr> <tr> <td style="text-align: center; width: 295px; height: 28px;"> <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" Font-Size="9pt" RepeatDirection="Horizontal" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"> <asp:ListItem Selected="True">分离数据库</asp:ListItem> <asp:ListItem>附加数据库</asp:ListItem> </asp:RadioButtonList></td> </tr> <tr style="font-size: 12pt"> <td style="height: 6px; text-align: center; width: 295px;"> </td> </tr> <tr style="font-size: 12pt"> <td rowspan="1" style="text-align: center; width: 295px; height: 3px;"> <table cellpadding="0" cellspacing="0" width="100%" style="height: 11px"> <tr> <td style="width: 100px; text-align: center"> <span style="font-size: 9pt; color: #9933ff;">查看数据库名</span></td> <td style="text-align: left"> <asp:DropDownList ID="dropSqlName" runat="server" Font-Size="9pt" Width="190px"> </asp:DropDownList></td> </tr> </table> </td> </tr> <tr> <td style="text-align: center; width: 295px;"> <asp:Panel ID="Panel2" runat="server" Height="5px" Width="100%"> <table cellpadding="0" cellspacing="0" style="width: 125%; height: 123px"> <tr> <asp:Image ID="Image2" runat="server" ImageUrl="~/imgae/fenli.bmp" /> </tr> <tr> <td style="width: 100px; height: 25px; text-align: center"> <span style="font-size: 9pt; color: #9966ff;">分离操作:</span></td> <td style="height: 25px; text-align: left;"> </td> </tr> </table> </asp:Panel> <asp:Panel ID="Panel1" runat="server" Visible="False" Width="100%" Height="5px"> <table cellpadding="0" cellspacing="0" width="100%"> <tr> <asp:Image ID="Image1" runat="server" ImageUrl="~/imgae/fujia.bmp" /> </tr> <tr> <td style="width: 100px; height: 21px; text-align: center;"> <span style="font-size: 9pt; color: #9900ff;">添加附加数据库名称</span></td> <td style="height: 21px; text-align: left;"> <asp:TextBox ID="TextBox1" runat="server" Width="75px"></asp:TextBox><span style="font-size: 8pt; color: #ff6666;">(*需手动添加)</span></td> </tr> </table> <asp:Label ID="Label1" runat="server" Font-Size="9pt" Text="*添加MDF文件" ForeColor="#FF3300"></asp:Label><asp:FileUpload ID="fileShow" runat="server" Font-Size="9pt" Width="174px" /><br /> <asp:Label ID="Label2" runat="server" Font-Size="9pt" Text="*添加LDF文件" Width="79px" ForeColor="#FF3300"></asp:Label><asp:FileUpload ID="FileUpload1" runat="server" Font-Size="9pt" Width="174px" /></asp:Panel> <table cellpadding="0" cellspacing="0" style="width: 99%; height: 239px;"> <tr> <td style="height: 1px; text-align: center;"> <asp:Panel ID="Panel3" Height="5px" runat="server"> <asp:ImageButton ID="fenliimgbtn" runat="server" ImageUrl="~/imgae/116.bmp" onclick="fenliimgbtn_Click" /> </asp:Panel> <asp:Panel ID="Panel4" runat="server" Height="5px" Visible="False"> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/imgae/017.JPG" onclick="fujiaimgbtn_Click" /> </asp:Panel> <asp:ValidationSummary ID="ValidationSummary1" runat="server" Font-Size="9pt" ShowMessageBox="True" ShowSummary="False" /></td> </tr> </table> </td> </tr> </table> </div> </form> </body> </html>
Default.aspx.cs
View Code
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) { //调用自定义方法GetDataSet实现绑定数据库名称 GetDataSet(); } //在RadioButtonList按钮选项如果选择的是“附加数据库”选项执行如下代码 if (this.RadioButtonList1.SelectedIndex == 1) { //显示包含是上传数据库文件控件的Panel1控件 this.Panel1.Visible = true; //隐藏包含分离数据库操作的控件的Panel2控件 this.Panel2.Visible = false; //隐藏包含分离数据库的按钮的Panel3控件 this.Panel3.Visible = false; //显示包含附加数据库的按钮的Panel4控件 this.Panel4.Visible = true; } //在RadioButtonList按钮选项如果选择的是“分离数据库”选项执行如下代码 else { this.Panel2.Visible = true; this.Panel1.Visible = false; this.Panel3.Visible = true; this.Panel4.Visible = false; } } //自定义GetDataSet()主要用来实例数据的绑定 public void GetDataSet() { //创建数据连接对象 SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]); //创建数据适配器 SqlDataAdapter da = new SqlDataAdapter("select name from sysdatabases", con); //创建数据集 DataSet myds = new DataSet(); //打开数据库连接 con.Open(); //填充数据集 da.Fill(myds, "x"); //关闭数据连接 con.Close(); //指定绑定数据名称的DropDownList控件的数据源 this.dropSqlName.DataSource = myds; //指定文本内容的数据源字段 this.dropSqlName.DataTextField = "name"; //指定值的数据源段 this.dropSqlName.DataValueField = "name"; //从数据源中绑定数据 this.dropSqlName.DataBind(); } protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) { if (RadioButtonList1.SelectedValue == "分离数据库") { //调用GetDataSet()方法重新来绑定数据 GetDataSet(); } } protected void btnBackup_Click(object sender, ImageClickEventArgs e) { //定义分离数据表的T-SQL命令的字符串 string cmdtxt = "sp_detach_db @dbname='" + this.dropSqlName.SelectedValue + "'"; //定义数据库连接字符串 SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]); 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, ImageClickEventArgs e) { //获取上传数据库文件的.mdf文件路径 string path1 = this.FileUpload1.PostedFile.FileName; //获取上传数据库文件的.ldf日志文件路径 string path2 = this.fileShow.PostedFile.FileName; string cmdtxt1 = ConfigurationSettings.AppSettings["strCon"]; //定义附加数据库的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 fujiaimgbtn_Click(object sender, ImageClickEventArgs e) { //获取上传文件的路径 string path1 = this.FileUpload1.PostedFile.FileName; string path2 = this.fileShow.PostedFile.FileName; string cmdtxt1 = ConfigurationSettings.AppSettings["strCon"]; //定义附加数据库的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 fenliimgbtn_Click(object sender, ImageClickEventArgs e) { //定义分离数据表的T-SQL命令的字符串 string cmdtxt = "sp_detach_db @dbname='" + this.dropSqlName.SelectedValue + "'"; SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]); 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()); } } }