Thursday, February 16, 2017

SQL Error: Exclusive access could not be obtained because the database is in use.

When restoring a database, one of the things you need to do is to ensure that you have exclusive access to the database.  If any other active connection is established with database then restore may get failed.

When trying to do a restore, if any other active connection is established with database you will see these types of error messages:

System.Data.SqlClient.SqlError:  Exclusive access could not be obtained because the database is in use. (Microsoft.SqlServer.Smo)

Resolution:

Make the database in single user mode and then do the restore

ALTER DATABASE AdventureWorks SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
RESTORE DATABASE AdventureWorks FROM DISK = 'C:\AdventureWorks.BAK' 
GO

No comments:

Post a Comment