在网上搜索了找到了小白的Blog上转了一个大富翁论坛上的讨论,看了讨论后发现里面提供的方法还不能直接用于ASP.NET。我对代码进行了稍稍的改动
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;";
string pai = "use master;ALTER DATABASE Wy SET OFFLINE WITH ROLLBACK IMMEDIATE;ALTER DATABASE Wy SET ONLINE WITH ROLLBACK IMMEDIATE";
SqlConnection conn = new SqlConnection(ConnStr);
SqlCommand com1 = new SqlCommand(pai, conn);
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
{
conn.Open();
com1.ExecuteNonQuery();
conn.Close();
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
infoLabel.Text = "恢复成功!<br>已经把备份"+path+"恢复到数据库";
}
catch(Exception ex)
{
infoLabel.Text = "恢复失败!<br>出错信息如下:<br>" + ex.ToString();
}
finally
{
myCommand.Connection.Close();
}
}
欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛
https://bbs.txwb.com
关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩
|
本文来源:vczx 作者:佚名