How can I programmatically drop a SQL Server database from .NET code

Posted by Craig Shearer on Stack Overflow See other posts from Stack Overflow or by Craig Shearer
Published on 2010-03-19T03:51:12Z Indexed on 2010/03/19 4:11 UTC
Read the original article Hit count: 245

Filed under:
|

I'm trying to drop a SQL Server database from .NET code. I've tried using the SMO classes but get an exception saying the database is in use.

Then I tried executing a query (opening a SqlConnection, executing a SqlCommand), along the lines of:

ALTER DATABASE foo SET SINGLE_USER WITH ROLLBACK IMMEDIATE

(pause)

DROP DATABASE foo

But still I get an exception saying the database is in use.

How do I do this? (Or, how does SQL Server Management Studio implement the Drop database and close existing connections?)

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2005