Set database to SINGLE_USER mode do something and set MULTI_USER mode. Alter database mode using simple T-SQL statements.
USE [master]
GO
ALTER DATABASE [] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
-- do some magic here
GO
ALTER DATABASE [] SET MULTI_USER;
GO