SqlServer2005 恢复数据库时出现Exclusive access could not be obtained because the database is in use 的解决方法

本文介绍了解决SQL数据库恢复过程中遇到的“无法获得独占访问权限”错误的方法。提供了两种实用方案,一种是在SQL Server Management Studio中操作,另一种是通过运行SQL命令将数据库设置为单用户模式,以便成功恢复数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I kept running into issues when I tried to restore a database from a backup file.  The error I kept getting was:

Restore failed for Server ‘localhost/SQLExpress’.

Additional Information:
  System.Data.SqlClient.SqlError: Exclusive access could not be
obtained because the database is in use.

For local sql instances, it’s not a huge deal since I can just restart the db server and clear the connections.  But in a managed/shared environment, that’s not possible.  I googled quite a bit for a solution, but didn’t really find anything that would work.  So I asked the newsgroups for some help and they came up with putting the db in single user mode before restoring the db.  That was cool and all but ended up locking me out of the db at my hoster.  So what I had to do was run the single user mode command and the restore command as one query.  Something like:

ALTER DATABASE  [your_db_name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE

RESTORE DATABASE [your_db_name] FROM  DISK = N’C:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/Backup/your_db_name.bak’ WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 10
GO

The restore line can automatically be scripted for you by Sql Management Studio.  Just click the Script icon at the top of the restore dialog.  Maybe MS will add an option to clear connections on restore in a future version of the management studio… One other note is that the restore command will automatically set the restored database in multiuser mode.

Hope this helps someone… 

 

转自:http://biasecurities.com/blog/2007/how-to-restore-a-db-when-the-db-is-in-use/

 

由于恢复数据库时需要对数据库进行独占的访问,在恢复之前你必须中止其他用户与数据库的连接。

解决方法:
在恢复数据库前:
方法一.打开Management Studio.
    a). 右键点击你的数据库,Task ->Take Offline.
    b). 右键点击你的数据库,Task ->Bring Online.

方法二.执行如下的Query:

    Use Master

    Alter Database [YOURDB]

        SET SINGLE_USER With ROLLBACK IMMEDIATE


    在恢复数据库后如果需要恢复会普通多用户模式:

    Use master;
    Go

    Alter Database [YOURDB]

        SET MULTI_USER

    Go

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ftkghost/archive/2008/09/05/2885682.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值