Cannot drop database because it is currently in use

后端 未结 17 1482
北恋
北恋 2020-12-07 11:50

I want to drop a database. I have used the following code, but to no avail.

public void DropDataBase(string DBName,SqlConnection scon)
{
    try
    {
               


        
17条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 12:51

    It's too late, but it may be useful for future users.

    You can use the below query before dropping the database query:

     alter database [MyDatbase] set single_user with rollback immediate
    
     drop database [MyDatabase]
    

    It will work. You can also refer to

    How do I specify "close existing connections" in sql script

    I hope it will help you :)

提交回复
热议问题