Search code examples
sql-servermsbuildsqlcmd

Drop DB using MSBuild even if in use


I am using the following command in my MSBuild file to drop a database

sqlcmd -E -S <ServerName> -Q "DROP DATABASE <DBName>"

But sometimes I get the error

Cannot drop database because it is currently in use.

What command should I use so that the DB is dropped even if it is in use?


Solution

  • To kill all connections to your database. See Kill All Active Connections To A Database

    Now issue your DROP DATABASE command after the above procedure.