簡體   English   中英

打開數據庫時“無法打開物理文件”

[英]“Unable to open physical file” when opening the database

我正在使用C#/ ASP.NET開發一個項目,我遇到了一個我無法解決的錯誤。 我有一個數據庫,我在其中創建了一些表,每當應用程序嘗試打開數據庫時,我都會收到以下錯誤:

"Unable to open the physical file "path\App_Data\Cars.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)". An attempt to attach an auto-named database for file path\App_Data\Cars.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

我安裝了SQL Express 2014,命名實例'SQLEXPRESS',在Visual - >選項 - >工具 - >數據連接中編寫了此實例ID。 在web.config文件中,我有:

< connectionStrings>        
< add name="ConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='path\App_Data\Cars.mdf';Integrated Security=True;User Instance=True"/>      
< add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='path\App_Data\AspnetDB.mdf';Integrated Security=True;User Instance=True"/> 
< /connectionStrings>

我試圖打開數據庫的方式是:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Cars.mdf;Integrated Security=True");
con.Open();

你能告訴我該怎么辦? 謝謝!

UPDATE:管理解決它,SqlConnection中的參數應該與web.config中的ConnectionString相同。 現在有另一個:當我嘗試打開Visual Studio提供的默認數據庫(使用Users,Roles,ecc)時,我收到以下錯誤:數據庫“path \\ App_Data \\ AspnetDB.mdf”無法打開,因為它是版本852.此服務器支持版本782及更早版本。 不支持降級路徑。 無法打開新數據庫“path \\ App_Data \\ AspnetDB.mdf”。 CREATE DATABASE被中止。 嘗試為文件'path \\ App_Data \\ AspnetDB.mdf附加自動命名的數據庫失敗。 存在一個具有相同名稱的數據庫,或者無法打開指定的文件,或者它位於UNC共享上。“有什么想法嗎?

從OP的編輯到問題:

管理解決它,SqlConnection中的參數應該與web.config中的ConnectionString相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM