Unable to open the physical file failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share

巧了我就是萌 提交于 2020-01-03 04:58:08

问题


I have developed and website as for my assignment purpose and then my teacher asked me that he doesn't have SQL Server to run separately my database file so I must have to make built in database. I then added the .mdf file to the App_data folder and used this connection string:

<add name="dbCanberraConnectionString1"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|latestcanberra.mdf;"
     providerName="System.Data.SqlClient" />

in web.config. But when I tried to run my website it shows an error

Unable to open the physical file "D:\Complete Assignment\Alkandi Final Work\App_Data\latestcanberra.mdf". Operating system error 5: "5(Access is denied.)

An attempt to attach an auto-named database for file D:\Complete Assignment\Alkandi Final Work\App_Data\latestcanberra.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

I am totally stuck with this error from last 3 hours even I tried different methods by searching solutions from different blogs etc

Any idea how to solve this out within less time ?


回答1:


Give full control to your "Authenticated Users". If there is no this user in the security settings then add it.

In case you are wondering how to do this --- I am on Windows 7 and the steps go like this:

Right-click on the MDF file and click properties. Select the "Security" tab and select your "Authenticated Users" or click 'Edit' Button then 'Add' button then write 'Autheticated Users' and then 'Check Names' button. Then 'OK' button. Click "Edit" and select the "Allow" check-box for "Full Control". OK all the way out.




回答2:


Add a connection string like in the code below. Add only User Instance=True:

<appSettings>
    <add key="dbconn" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB_Garment.mdf;User ID=sa;Password=123;Integrated Security=True;Connect Timeout=30;User Instance=True"/>
</appSettings>


来源:https://stackoverflow.com/questions/20459228/unable-to-open-the-physical-file-failed-a-database-with-the-same-name-exists-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!