You might receive "Database could not be exclusively locked to perform the operation" or many error like such when you are trying to perform operation on database which is already been used by process or some other users.
In order to obtain "Exclusive lock" to perform some critical database operation when someone is already using database you can perform following steps as described below to solve your problem.
Earlier I have blogged in context of restore error, but it is not limited to only restore and since this is very common error you get stuck when you are trying to do some critical database operation i have decided to explain step by step so that it can help me and many others like me who are in same situation. :)
Solution to obtain exclusive locked to perform database operations.
To do so: File > Disconnect Object Explorer
Step 2: Connect Connection
To do so: File > Connect Object Explorer
Step 3: Open "New Query" window and run following command
use master
Note: Above command will make your current database to master which is important before we run following sequence of command.
Step 4: Copy and paste following sequence of command in Query window. Replace the word "MyDatabaseName" with Database name you are trying to get exclusive access.