大众计算机学习网欢迎诸位!收藏本站注 册登 陆
常用教程:基础知识网络知识操作系统WPS网页制作数据库算法网页成功之路网络安全最新技术古典mid流行midmid背景下载中心
您现在的位置:首页 > 教程 > 文章页

备份与恢复数据库

Admin | 2007-10-11 13:29:51 | ReadNums | 3986 | 标签 asp.net | 打印本页
     

<% @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>


问题未解决:在线咨询我要在线咨询问题

网友评论

(访客)
内容实用原创,讲得很好。
20xx年x月x日
(站长)
有问题请在线咨询。
20xx年x月x日