Previous reply should work with the ALTER DATABASE commands.
However, if you are getting db in use errors a quick way of solving this is to do the following:
Basically, this switches the db into single user mode. This disconnects any other connections. You then put your code in the bit "Put your code here". Finally, the db is set back to normal multi user mode. I have inclded the error trap so that if any errors occur the db will always go back to multuser mode as oposed to leaving the db in single user mode.
BEGIN TRY
ALTER DATABASE [DB NAME] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Put your code here
ALTER DATABASE [DB NAME] SET MULTI_USER
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() as ErrorState,
ERROR_PROCEDURE() as ErrorProcedure,
ERROR_LINE() as ErrorLine,
ERROR_MESSAGE() as ErrorMessage
ALTER DATABASE [DB NAME] SET MULTI_USER
END CATCH