Using SMO to drop a SQL Database

Posted by ybbest on YBBest See other posts from YBBest or by ybbest
Published on Fri, 07 May 2010 04:24:39 +0000 Indexed on 2010/12/30 22:01 UTC
Read the original article Hit count: 342

Filed under:

SQL Server Management Objects(SMO) is the API you can use to manipulate the sql server,like create databse and delete database.
To get more details you can check the msdn documentation.
There are 2 ways you can drop a database
1. You could create a Database object and call Drop method:


Dim database As Database = New Database(Your database name)
 database.Drop()

2.However if you have existing connections to the database ,attempting to drop it using the above method will fail.Recall that when you try to drop the database from management studio ,you can tick the check box to close all the connections before drop the database.It is not so obvious , but you can do the exact same thing using SMO:


Dim server As Server= New Server(ServerConn)
                server.KillAllProcesses(Your database name)
                server.KillDatabase(Your database name)


Shout it


© YBBest or respective owner

Related posts about Uncategorized