Wednesday, November 21, 2012

Restore failed for server - system.data.sqlclient.sqlError: exclusive access could not be obtained because database is in use


Error Readout:
Restore Failed for Server -- System.Data.SqlClient.SqlError:Exclusive access could not be obtained because the database is in use

Solution-1:
Run this SQL

USE master
GO
ALTER DATABASE <database name>
SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER DATABASE <database name>
SET ONLINE

Explanation:
Are you trying to restore a database. Slow down now buddy, there might be other people doing things to that database right now too. You could be polite, run a sp_who, see who's using your database and ask them if it would be ok to do a restore, but who are we kidding it's much more fun/quick to just kick them off. That's exactly what the above statement does.



Solution-2: 
Close all the applications which have a chance to access your database and try to restore. If you get same error after doing this then you can take your database offline by right click on the database. Go to Tasks and click on Make Offline. Once this done. Make it Online with the same manner and try to restore again. 

Solution-3:
Change the database access mode to single user.

USE 
MASTER 
GO      

ALTER DATABASE MyDatabase  
SET SINGLE_USER WITH ROLLBACK IMMEDIATE   
Go      

RESTORE DATABASE MyDatabase  
FROM DISK = N'[file address]'   
Go      
ALTER DATABASE MyDatabase  
SET MULTI_USER with ROLLBACK IMMEDIATE   
Go 

No comments:

Post a Comment