Using SMO to drop a SQL Database
- by ybbest
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)