Admin_Backup.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Admin_Backup.aspx.cs" Inherits="Admin_Admin_Backup" %> <!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 runat="server"> <title>数据库备份</title> <link href="../Css/Style.css" mce_href="Css/Style.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="Form1" method="post" runat="server"> <div> <div class="BodyBlock PTB5"> <div class="Block Hidden"> <div class="Title H22 LH22 TC"> 数据库备份 </div> <div class="M1 Middle"> <div class="BT Hidden LH25" style="width: 100%"> <div class="Left BR TR" style="width: 400px"> 数据库列表: </div> <div class="Left" style="margin-left: 1px" mce_style="margin-left: 1px"> <asp:DropDownList ID="dbDropDownList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="dbDropDownList_SelectedIndexChanged"> </asp:DropDownList></div> </div> <div class="BT Hidden" style="width: 100%"> <div class="Left BR LH25 TR" style="width: 400px"> 请输入备份目录及备份文件名: </div> <div class="Left BL" style="padding-left: 5px; margin-left: -1px" mce_style="padding-left: 5px; margin-left: -1px"> <asp:TextBox ID="pathTextBox" runat="server" Width="224px">F:/huitong/Backup/Northwind.bak</asp:TextBox>(目录必须存在) </div> </div> <div class="Footer BT P5 TC Hidden"> <asp:Button CssClass="Button" ID="backupButton" Text="备份数据库" runat="server" OnClick="backupButton_Click" /> </div> <div class="BT Hidden LH25" style="width: 100%"> <div class="Left BR TR" style="width: 400px"> </div> <div class="Left BL" style="margin-left: 1px" mce_style="margin-left: 1px"> <asp:Label ID="infoLabel" runat="server" ForeColor="red"></asp:Label></div> </div> </div> </div> </div> </div> </form> </body> </html> Admin_Backup.aspx.cs 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; using System.IO; public partial class Admin_Admin_Backup : System.Web.UI.Page { //数据库连接字符串 public static string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //创建连接及执行数据库操作 string db_query = "sp_helpdb"; SqlCommand myCommand = new SqlCommand(db_query, new SqlConnection(ConnStr)); 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 = @"F:/huitong/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 @dbname to disk = @path;"; SqlCommand myCommand = new SqlCommand(backupSql, new SqlConnection(ConnStr)); myCommand.Parameters.Add("@dbname", SqlDbType.Char); myCommand.Parameters["@dbname"].Value = dbname; myCommand.Parameters.Add("@path", SqlDbType.Char); myCommand.Parameters["@path"].Value = path; if (File.Exists(this.pathTextBox.Text.Trim())) { infoLabel.Text = "此文件已存在,请从新输入!"; //Response.Write("<mce:script language=javascript><!-- alert('此文件已存在,请从新输入!');location='Default.aspx' // --></mce:script>"); return; } try { myCommand.Connection.Open(); myCommand.ExecuteNonQuery(); infoLabel.Text = "备份成功!"; } catch (Exception ex) { infoLabel.Text = "备份失败<br/>" + ex.ToString(); } 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 @dbname from disk = @path;"; SqlCommand myCommand = new SqlCommand(restoreSql, new SqlConnection(ConnStr)); myCommand.Parameters.Add("@dbname", SqlDbType.Char); myCommand.Parameters["@dbname"].Value = dbname; myCommand.Parameters.Add("@path", SqlDbType.Char); myCommand.Parameters["@path"].Value = path; try { myCommand.Connection.Open(); myCommand.ExecuteNonQuery(); infoLabel.Text = "恢复成功"; } catch (Exception ex) { infoLabel.Text = "恢复失败<br/>" + ex.ToString(); } finally { myCommand.Connection.Close(); } } }