A database with the same name exists, or specified file cannot be opened, or it is located on UNC share

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

Getting this error when I run my project in new PC. TO avoid this I have to copy paste the new Connection string every time .. Is there any way to avoid that ... I have 3 different database and its very annoying O_O

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Users\\Yoro\\Desktop\\WAPent 3.0 (1)\\WAPent 3.0\\WAPent 3.0\\WAPent 2.0\\WAPent 2.0\\App_Data\\LoginStuff.mdf;Integrated Security=True;User Instance=True"); 

Web Config code

    <connectionStrings>   <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"    providerName="System.Data.SqlClient" />   <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LoginStuff.mdf;Integrated Security=True;User Instance=True"    providerName="System.Data.SqlClient" />  </connectionStrings> 

回答1:

In the web.config you are using |DataDirectory| which is a substitution for the path to the datadirectory. This is set by using

AppDomain.CurrentDomain.SetData("DataDirectory", newpath) 

When you do not set |DataDirectory| it defaults to the App_Data folder when it is a web project. So looking at your code the path represented by DataDirectory probably does not contain the database file.

For more info about datadirectory have a look at this (older) article.



回答2:

Open server explorer, right-click on your database, select properties on bottom right corner. A property window will appear; copy the connection string there and use it in your application. For example, in my application:

SqlConnection sconnection = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Taha\Documents\Visual Studio 2013\Projects\Finder\Finder\App_Data\Userdb.mdf; Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");  sconnection.Open(); 


回答3:

This issue generally occurs in VS 2012 which I too have faced it. In order to resolve it,you need to foolow these steps: 1)Right Click on the .mdf file ->Select Modify connections under server Explorer. 2)Popup window will come->Click Advanced Button ->Select (LocalDB)\v11.0 as DataSource value

Also make sure you make changes in webconfig file as well for DataSource=(LocalDB)\v11.0 You are done.Wella!!



回答4:

First, your Database must be in other location than bin folder from your project; Then, your connection must be declared like this:

SqlConnection con = new SqlConnection(TABLE1TableAdapter.Connection.ConnectionString); 

If you have a DataGridView you can click on Choose Data Scource, and add your table as source from your database.(In my example, the table is named TABLE1.) Then, visual studio automatly create a DataSet, DataBinding, and a TableAdapter. The TableAdapter (TABLE1TableAdapter) contains as property the ConnectionString you need for database. P.S. I had this problem, and after a lot of work, i find this solution. I hope it helps you too.



回答5:

You should check your connection string. when you try to attach sql file the connection string should be like that: DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|PharmacyDB.mdf;Database=PharmacyDB;Integrated Security=True



回答6:

I get this error when I use two connection string in my same DBContext,for example I try to Connect to sql server in a connection string and connect to sql express file in another connection string:

    public MyDBContext(bool autoDetectChangesEnabled)     : base("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\MyDB.mdf;User Instance=false;Integrated Security=True;MultipleActiveResultSets=True")     {         Initialize(autoDetectChangesEnabled);     }      public MyDBContext()      : base("data source=(LOCAL)\\SQLEXPRESS;initial catalog=MyDB;persist security info=True;user id=user;password=pass;MultipleActiveResultSets=True;App=EntityFramework")     {         Initialize(true);     } 


回答7:

Exit Visual Studio.

Run Visual Studio again.

Do not open your project from last program list, open it using the open project -> browse procedure.

Go to server explorer: if your .mdf file icon has a little red X, then create a new table, no need to enter anything in the table. Just update it.

First you will see a prompt: choose NO.

In the second prompt, choose YES: the little red X should go away.

Delete the table you just created and update.

This solved the problem for me.



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