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

Error:
Restore Failed For Server 'MyServer' (Microsoft.SqlServer.Smo)
Additional information:
System.Data.SqlClient.SqlError: Exclusive access could not be obtained 
because the database is in use. (Microsoft.SqlServer.Smo)

Solution:


1.execute SP_WHO

EXEC sp_who

2.Execute KILL 53 /Kill 116

3.Restore from another database.

Right click on database which you want to restore

4.Done.

OR

Try below code to fix "System.Data.SqlClient.SqlError: Exclusive access could not be obtained because the database is in use" error

USE MASTER;

ALTER DATABASE MyDb1 SET SINGLE_USER WITH ROLLBACK IMMEDIATE ;

RESTORE DATABASE MyDb1 FROM DISK = N'[file path]';

ALTER DATABASE MyDb1 SET MULTI_USER with ROLLBACK IMMEDIATE;