如果删除了.mdf,则在删除LocalDb数据库时找不到指定的文件 [英] Cannot find file specified when dropping LocalDb database if .mdf was deleted
问题描述
我正在尝试编写一个将在应用程序的文件夹中找不到本地数据库的应用程序.我在删除.mdf
I am trying to write an application that will create a local database if it's not found in the application's folder. I run this query after deleting the .mdf
IF EXISTS (SELECT * FROM sys.databases WHERE name = N'Test_db')
BEGIN
DROP DATABASE Test_db
END
CREATE DATABASE Test_db
ON PRIMARY (NAME=Test_db, FILENAME='...\Test_db.mdf')
我的command.ExecuteNonQuery()
抛出一个异常,即使它删除了数据库并创建了一个新的异常.该错误来自命令的DROP DATABASE
部分.
My command.ExecuteNonQuery()
throws an exception, even though it drops the database and creates a new one. The error comes from the DROP DATABASE
part of the command.
其他信息:无法打开物理文件 "... \ Test_db.mdf".操作系统错误2:"2(系统无法 找到指定的文件.)".
Additional information: Unable to open the physical file "...\Test_db.mdf". Operating system error 2: "2 (The system cannot find the file specified.)".
文件激活失败.物理文件名 "... \ Test_db_log.ldf"可能不正确.
File activation failure. The physical file name "...\Test_db_log.ldf" may be incorrect.
我发现了此问题,但无法解决问题.
I found this question, but it has no solution to the problem.
推荐答案
问题的解决方案是sp_detach_db
,因为它从服务器上删除了数据库,而没有从文件系统中删除文件
The solution to the problem was to sp_detach_db
because it removes the database from the server without deleting files from the file system
EXEC sp_detach_db 'Test_db'
这篇关于如果删除了.mdf,则在删除LocalDb数据库时找不到指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!