Sorry, I don't visit here that often. This is what I use and yes, there is a small timing window that someone could reestablish a connection. You could check the db.activeconnections and if it is greater than zero, retry the sequence. The offline clears active connections "gracefully" and the online seems to tell SQL that it can take control of the database filegroups and files again. Without the db.online the files are not deleted.
[p]Regards, Ira Grollman, OMKT LLC[/p]
'requires references in your project to the two dll's that these live in
Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.Smo
'pick whatever server you are using...
Dim smoServer As Server = New Server(".\SQLExpress")
Public Function blnDropDatabase(ByVal strName As String, ByVal smoServer As Server) As Boolean
For Each db As Microsoft.SqlServer.Management.Smo.Database In smoServer.Databases
If LCase(db.Name) = LCase(strName) Then
Try
db.SetOffline()
db.SetOnline()
db.Drop()
Catch ex As Exception
'Unable to drop
Return False
End Try
Return True
End If
Next
Return False
End Function