SQL LocalDB-删除数据库的文件后无法删除它

问题描述:

如何删除已删除文件的SQL LocalDB数据库?

How can I delete a SQL LocalDB database that has had its files delete?

删除数据库将产生以下消息:

Dropping the database yields this message:

无法打开物理文件"C:\ Users \ Public \ Documents \ LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80.mdf".操作系统错误2:"2(系统找不到指定的文件.)".

Unable to open the physical file "C:\Users\Public\Documents\LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80.mdf". Operating system error 2: "2(The system cannot find the file specified.)".

文件激活失败.物理文件名"C:\ Users \ Public \ Documents \ LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80_log.ldf"可能不正确.

File activation failure. The physical file name "C:\Users\Public\Documents\LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80_log.ldf" may be incorrect.

运行master.sp_databases实际上不会显示它们,但是Management Studio会显示它们.

Running master.sp_databases actually doesn't show them, but the Management Studio does.

我假设您使用的是与SQL Server 2012捆绑在一起的(localdb).

I'm assuming you're using (localdb) bundled with SQL Server 2012.

如果您使用的是SQL Server 2014,请使用(localdb)\ MSSQLLocalDB 代替下面的(localdb)\ v11.0

If you're using SQL Server 2014, use (localdb)\MSSQLLocalDB in place of (localdb)\v11.0 below

  • 打开命令提示符
  • 如果尚未运行localDb实例,请启动它:C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqllocaldb.exe start v11.0″
  • 通过运行以下命令来删除localDb数据库:C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd -S (localdb)\v11.0 -E -d master -Q DROP DATABASE [myDatabase]
  • 您现在可以停止localDb服务:C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqllocaldb.exe stop v11.0″
  • Open a command prompt
  • Start the localDb instance if it is not already running: "C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqllocaldb.exe" start "v11.0″
  • Drop the localDb database by running the following command: "C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd" -S (localdb)\v11.0 -E -d master -Q "DROP DATABASE [myDatabase]"
  • You can stop the localDb service now: "C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqllocaldb.exe" stop "v11.0″

来源: http://kazimnami.azurewebsites.net/techblog/2013/02/27/delete-localdb-database-after-physical-files-have-been-deleted/