title: How can I use variable in connection string?
body: <p>I'm having trouble with variables for connection strings. In my app.config database path seems fine but on form, I'm getting error for that connection string. When I try to add:</p>
  
  <pre><code>_connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; " +
      "AttachDbFilename = \"|DataDirectory|\\gazi_db.mdf\"; " +
      "Integrated Security = True; Connect Timeout = 30";
  </code></pre>
  
  <p>the database won't work correctly; it can't save data on exit. However this Works fine:</p>
  
  <pre><code>connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; " +
          "AttachDbFilename = \"C:\\Users\\Can\\Desktop\\c_sharp_gazi_installer" +
          "\\Gazi Installer\\gazi_installer\\gazi_db.mdf\"; " +
          "Integrated Security = True; Connect Timeout = 30";
  </code></pre>
  
  <p>How can I fix this? </p>
  
  <p><strong>EDIT:</strong> I hoped this would work</p>
  
  <pre><code>string DataDirectory = "";
  string folder = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
  AppDomain.CurrentDomain.SetData("DataDirectory", folder);
  
  _connectionString = _connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; " +
          "AttachDbFilename = \"" + DataDirectory + "\\gazi_db.mdf\"; " +
          "Integrated Security = True; Connect Timeout = 30";
  </code></pre>
  
  <p>But it's still giving me sqlclient sqlexception error.</p>
  
  <p><strong>EDIT2:</strong> This error indicates connection open. I don't understand why this doesn't work either:</p>
  
  <pre><code>SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  builder["Data Source"] = "(LocalDB)\\MSSQLLocalDB";
  builder["AttachDbFilename"] = @"C:\Users\Can\\Desktop\c_sharp_gazi_installer\Gazi Installer\gazi_installer\gazi_db.mdf";
  builder["Integrated Security"] = true;
  builder["Connect Timeout"] = 30;
  </code></pre>
  
  <p>Please someone help. I'm going to go mad.</p>