我试图强制删除数据库,但在删除数据库后,当我尝试重新创建数据库时,我收到了错误
cannot create file C:\Program Files…..[databasename].mdf because it already exists
Use master; ALTER database [databasename] set offline with ROLLBACK IMMEDIATE; DROP database [databasename];
我明白,上面的查询正在删除数据库,但它没有删除.ldf和.mdf文件.如何彻底删除数据库?
一般的查询
Drop database [databasename] ; //deletes the database completely,including the ldf and mdf's.
解决方法
这是预期的和
documented behavior:
Dropping a database deletes the database from an instance of sql Server and deletes the physical disk files used by the database. If the database or any one of its files is offline when it is dropped,the disk files are not deleted. These files can be deleted manually by using Windows Explorer. To remove a database from the current server without deleting the files from the file system,use sp_detach_db.
那么为什么要先将数据库脱机?只需将其设置为SINGLE_USER模式,然后按照sql Server联机丛书中的说明将其删除.-
USE master; ALTER DATABASE [databasename] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [databasename] ;