doing the alter database [dbname] set single_user with rollback immediate is great, but only if your connection is using the database.
if you are looping thru them all, i can see where some other process could grab the single use connection.
I've seen examples where they set to single user, then rename the database as teh next statement...(ie DBName_Restoring that way other process don't find the database any more.
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!