Dzwebs.Net

撰写电脑技术杂文十余年

备份与恢复数据库

Admin | 2007-10-11 13:29:51 | 被阅次数 | 6765

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

<% @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 = "&#65403;&#65430;&#65400;&#65396;&#65395;&#65417;&#65401;&#65382;";
}
catch(Exception ex)
{
infoLabel.Text = "&#65403;&#65430;&#65400;&#65396;&#65418;&#65383;&#65392;&#65436;<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="&#65393;&#65400;&#65399;&#65437;&#65418;&#63729;&#65406;&#65437;&#65407;&#12539;
OnClick="backupButton_Click"></asp:button>
<asp:button id="restoreButton" runat="server" Font-Size="9pt" Text="&#65403;&#65430;&#65400;&#65396;&#65418;&#63729;&#65406;&#65437;&#65407;&#12539;
OnClick="restoreButton_Click"></asp:button>
<br><br>
<asp:Label id="infoLabel" runat="server"></asp:Label>
</form>
</body>
</HTML>


该杂文来自: 网站开发杂文

上一篇:ASP.NET中的事务处理和异常处理

下一篇:批量更新数据库表中的数据

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计