title: asp.net connection string problem in web.config
body: <p>I have a little problem with my recent project. I have a connection string in my web.config but i'd like to access it in my sql specific class.</p>
  
  <p>My connection string is looks like this:</p>
  
  <p>"Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Joblication-20180902120147.mdf;Integrated Security=True"</p>
  
  <p>It's stored in my web.config file.</p>
  
  <p>My problem is that the default asp.net functions can access this database but i'd like to store other data in the database so i tried to access it with SqlConnection class. I set the ConnectionString property of the SqlConnection object:</p>
  
  <pre><code>SqlConncetion connection = new SqlConnection()
  connection.ConnectionString = "Data Source=(LocalDb)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\aspnet-Joblication-20180902120147.mdf;Integrated Security=True";
  </code></pre>
  
  <p>But i get this error everytime:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException: 'An attempt to attach an auto- 
    named database for file *.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'</p>
  </blockquote>
  
  <p>I replaced the name of the mdf file with a '*' so it is shorter and easily readable.</p>
  
  <p>If i understand this then my .mdf file is already attached to the MSSQLLocalDB so i should connect to the MSSQLLocalDB and i should be able to access the .mdf file somehow, right?</p>
  
  <p>When i'm trying this:</p>
  
  <pre><code>connection = new SqlConnection();
  connection.ConnectionString = "Data Source=(LocalDb)\\MSSQLLocalDB;Integrated Security=True";
  </code></pre>
  
  <p>Then it seems ok cause the connection is working now but my queries don't. My queries are trying to get data from the .mdf file's tables but the .mdf file is not specified in this connection.</p>
  
  <p>So how can i specify it?</p>