I try to restore a new database in MSSQL 2005 and hit error Restore Failed For Server 'xxxx' (Microsoft.SqlServer.Smo)Additional information:System.Data.SqlClient.SqlError: Exclusive access could not be obtained because the database is in use. (Microsoft.SqlServer.Smo)
This is mostly caused by others user/session is using the database at the same time. To check whether got others user accessing the same database, execute SP_WHO will show who is using the database. Execute KILL to kill the process. In my case, i want to kill process with spid = 53
execute SP_WHO -- See which process in running
Execute KILL 53 -- 53 is the process id spid that i want to kill before restore.
In my case, i restore the database using script instead of using the SSMS.
Below is the script that i use to restore without any issue.
RESTORE DATABASE RateMyPosts
FROM DISK = 'C:\Documents and Settings\DEV1\Desktop\RatePosts.bak' WITH REPLACE,
MOVE 'RatePosts' TO
'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\RatePosts.mdf',
MOVE 'RatePosts_Log' TO
'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\RatePosts_log.ldf'