Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

c#

asp.net

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>
like image 449
user2625486 Avatar asked Sep 05 '13 16:09

user2625486


1 Answers

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.

like image 82
Marco Avatar answered Sep 29 '22 13:09

Marco