0

I'm setting up a web service, I wanted to use the local database so I created a local SQL Server database in this project. The web service runs just fine on localhost, but the problem when I invoke a method to Load data from the local database on this project, I'm getting an error:

System.Data.SqlClient.SqlException:
An attempt to attach an auto-named database for file C:\Program Files (x86)\IIS Express\TourTravelDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

TourTravelDB.mdf is my local database

I've added a connection string in my web.config but it's still not working.

Here is my connection string.

<connectionStrings>
    <add name="CS" 
         connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\TourTravelDB.mdf;Integrated Security=True" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

I've also tried to move my database to the IIS Express directory, it works but when run it from another computer, I have to move the database again.

Does anyone have an idea how to solve this?

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388

1 Answers1

0

This may help you..

Add "User Instance=True;" in your connection string.

  <connectionStrings>
    <add name="aspnet_staterKits_Test_TimeTracker" 
         connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
    <remove name="LocalSqlServer"/>
    <add name="LocalSqlServer"
         connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
  </connectionStrings>

if doesn't work then try out the below links..

forums.asp.net

codeproject.com

Already existing thread in stack overflow

Thameem
  • 537
  • 7
  • 24