“Exclusive access could not be obtained because the database is in use.”
You can get this error because you can’t put an exclusive lock on the database because there are other sessions that use the database when you try to restore a database.
You can perform operations after killing active sessions in database.
Or you can perform this operation with the help of the following script. Note that this script will kill all sessions in the database.
ALTER DATABASE MyDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE MyDB....