Close Connections

USE master
ALTER DATABASE DBname SET SINGLE_USER WITH ROLLBACK IMMEDIATE 
 
-- your code 
 
ALTER DATABASE DBname SET MULTI_USER