When I’m using Management Studio Express to drop a database there is an checkbox "Close existing connections." which close open database and enforce it to drop.
How can i drop a database programmatic by closing open connections and check it’s ever exists before?
The output of the “script” icon on the drop screen, as suggested in the comments by Randolph Potter (this assumes that your database name is “test”):