数据库还原出错怎么处理
我在用了框架,登陆后,进入备份页面,还原的时候报错:
System.Data.SqlClient.SqlException: 因为数据库正在使用,所以未能获得对数据库的排它访问权。 RESTORE DATABASE 操作异常终止。 at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at health.aspx.BackupRestoreExample.restoreButton_Click(Object sender, EventArgs e) in d:\health\aspx\backuprestoreexample.aspx.cs:line 112
我在单独运行这个页面时,恢复成功了,放框架里面就出错了
代码如下
string path = Page.MapPath(pathTextBox.Text+ ".bak ");
string dbname = "health ";
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)
{
// msg= "alert( '备份失败 ') ";
infoLabel.Text = "恢复失败 <br> " + ex.ToString();
}
finally
{
myCommand.Connection.Close();
------解决方案--------------------看看跟这个相似不
http://topic.csdn.net/t/20020723/19/896535.html
------解决方案--------------------那是当然了啊,因为你正在使用当前数据库嘛?可以参考一下我这个写的!!
//取备份文件名称
string fileName=e.Item.Cells[2].Text;
//所要还原的数据库名称
string strsql=System.Configuration.ConfigurationSettings.AppSettings[ "constr1 "];
string bb=strsql.Substring(9,strsql.Length-9);
//数据库的用户名和密码
string a=ConfigurationSettings.AppSettings[ "constr2 "];
string user=a.Substring(4,a.Length-4);
string b=ConfigurationSettings.AppSettings[ "constr3 "];
string pass=b.Substring(4,b.Length-4);
SqlConnection conn=new SqlConnection( "server=.;database=master;uid= "+user+ ";pwd= "+pass+ " ");
conn.Open();
string path=Server.MapPath( "../backup/ ");
string pathName=path+fileName;
string sql= "Alter Database "+bb+ " Set OffLine With RollBack Immediate ;RESTORE DATABASE "+bb+ " FROM DISK = ' "+pathName+ " ' ";
SqlCommand cmd=new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();
conn.Close();
Response.Write( " <script language= 'javascript '> alert( '提示:数据库还原成功 ') </script> ");
------解决方案--------------------帮lz顶
------解决方案--------------------拱一下
------解决方案--------------------那是和框架有冲突
------解决方案--------------------