怎样做sql server数据库的复原[MSSQL防范]
本文“怎样做sql server数据库的复原[MSSQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
???? 本日由于毕业计划要做一个数据库备份和复原的页面.我参考了一段代码后实现了功效但是我发现这段程序有一个对比麻烦的地方,就是数据库的复原的时刻会碰到排它性问题.
在网上搜索了找到了小白的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 = "恢复成功!
已经把备份"+path+"恢复到数据库";
????}
????catch(Exception ex)
????{
??????????????????? infoLabel.Text = "恢复失利!
出错信息以下:
" + ex.ToString();
????}
????finally
????{
?????myCommand.Connection.Close();
????}
???}
本文地址: | 与您的QQ/BBS好友分享! |