0

This is not a duplicate question. I search all of stackoverflow for the answer. If it a duplicate, please point me to question that answers this. The answers I found do not solve the issue.

I have developed a website with c# code behind with the connection string as follows: In the webconfig of the asp.net website and and in the app.config of the service:

 <connectionStrings>
     <add name="UsersConnectionString" connectionString="Data Source=
 (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\[database].mdf;
   Integrated Security=True" providerName="System.Data.SqlClient" />
 </connectionStrings>

In both c# code behind and service I have the following:

  SqlConnection connSN = new 
  SqlConnection(ConfigurationManager.ConnectionStrings
 ["UsersConnectionString"].ConnectionString);

When I try to launch my service, I get the following:

Additional information: An attempt to attach an auto-named database for file mydatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

How can I attach to the database with multiple services AND asp.net code behind?

Brad Jarrett
  • 109
  • 3
  • 11
  • Igor, the link you sent if for multiple database connections to different databases. I am trying to use the same database with a connection between asp.net codebehind AND c# service app. – Brad Jarrett Sep 19 '17 at 14:25
  • 1
    Just stop using attached databases and simple .mdf files for this. Setup a normal database on the server and specify its name in the configuration file, so no attach on connection is actually done. This is a bad practice Visual Studio tutorials frequently encourage. – Alejandro Sep 19 '17 at 14:36
  • Thank you Alejandro. One question, I have always used attached...what would the connection string in the app.config look like? I tried removing the attached database and of course it ran but it couldn't find the table. Would it look like this: – Brad Jarrett Sep 19 '17 at 14:50
  • 1
    Use something like: **Data Source=(LocalDB)\MSSQLLocalDB\;Initial Catalog=YourDbName;Integrated Security=True**. Also, have a look at the [official documentation](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx) for all the valid connection string parameters. – Alejandro Sep 19 '17 at 15:19
  • Thank you Alejandro, it works great! – Brad Jarrett Sep 19 '17 at 17:26

0 Answers0