2016-12-04 201 views
1

我正在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(); 

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

更新:管理解決它,SqlConnection中的參數應該與web.config中的ConnectionString相同。現在還有另一個:當我嘗試打開Visual Studio提供的默認數據庫(使用用戶,角色,ecc)時,我收到以下錯誤:數據庫「path \ App_Data \ AspnetDB.mdf」無法打開,因爲它是版本852.此服務器支持版本782和更早版本。 downrade路徑不受支持。 無法打開新數據庫「path \ App_Data \ AspnetDB.mdf」。 CREATE DATABASE被中止。 嘗試爲file'path \ App_Data \ AspnetDB.mdf附加自動命名的數據庫失敗。具有相同名稱的數據庫不存在,或者指定的文件無法打開,或它位於UNC共享「任何想法

+0

不要連接到mdf文件你應該總是使用服務器中的數據庫名稱和連接字符串中的服務器IP(名稱)。 www.connectionstrings.com。 – jdweng

+0

你能告訴我我應該修改的位置嗎?SqlConnection實例中的AttachDbFilename?謝謝! – flaviumanica

+0

如果還有其他問題,請發一篇文章。 –

回答

0

從OP的編輯帶到了一個問題:?

設法解決它,爭論在SqlConnection中應該與在web.config中的ConnectionString相同