
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<HTML>
	<HEAD>
		<title>获取服务器端数据库列表示例</title>
		<script language="C#" runat="server">
		
			// 注意本节的数据库连接字符串
			string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer1"];
			
			void Page_Load(object sender, System.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();
				}
			}
			
			void dbDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
			{
				pathTextBox.Text = @"C:\BACKUP\" + dbDropDownList.SelectedValue + ".bak";
			}
			
			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;

				try
				{
					myCommand.Connection.Open();
					myCommand.ExecuteNonQuery();
					infoLabel.Text = "备份成功";
				}
				catch(Exception ex)
				{
					infoLabel.Text = "备份失败<br>" + ex.ToString();
				}
				finally
				{
					myCommand.Connection.Close();
				}
			}
			
			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();
				}
			}
		
		</script>
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<h3>获取服务器端数据库列表示例</h3>
				数据库列表：
				<asp:dropdownlist id="dbDropDownList" runat="server" AutoPostBack="True" 
					OnSelectedIndexChanged="dbDropDownList_SelectedIndexChanged"></asp:dropdownlist>
				<br><br>
				请输入备份目录及备份文件名：
				<asp:textbox id="pathTextBox" runat="server" Width="224px">
					C:\BACKUP\Northwind.bak</asp:textbox>（目录必须存在）
				<br><br>
				<asp:button id="backupButton" runat="server" Font-Size="9pt" Text="备份数据库" 
					OnClick="backupButton_Click"></asp:button>
				<asp:button id="restoreButton" runat="server" Font-Size="9pt" Text="恢复数据库" 
					OnClick="restoreButton_Click"></asp:button>
				<br><br>
				<asp:Label id="infoLabel" runat="server"></asp:Label>
		</form>
	</body>
</HTML>
