<% @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();
dbDropDownList.DataSource = dr;
dbDropDownList.DataTextField = "name";
dbDropDownList.DataBind();
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">
<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>