title: C# SQL Server Relative Path
body: <p>I'd like my program to reference a relative path to the <code>.mdf</code> file so it can run on a new machine without modifying the connection string.</p>
  <p>Environment:</p>
  <ul>
  <li>Microsoft Visual Studio Community 2019, Version 16.11.8</li>
  <li>Windows Forms</li>
  </ul>
  <p>The connection string is pulled from the <code>dbstring</code> class:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace Sharpening_Teeth
  {
      public class dbString
      {
          // modifyTemplate pulls in the connection string from this class
          public static string getConnectionString()
          {
              return &quot;Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\vamel\\OneDrive\\Desktop\\test\\Sharpening Teeth\\Sharpening Teeth\\templateDB.mdf&quot;;
          }
  
          public static string getReadQuery()
          {
              return &quot;SELECT template_id, template_name, message_content FROM dbo.message_template&quot;;
          }
      }
  }
  </code></pre>
  <p>I'm just getting back into programming and I'm stuck. Hopefully someone can point me in the right direction here.  Thank you.</p>
  <p>Threads reviewed</p>
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/1833640/connection-string-with-relative-path-to-the-database-file">Connection string with relative path to the database file</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/12932963/how-to-set-a-relative-path-to-a-sql-server-compact-database">How to set a relative path to a SQL Server Compact database?</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/3500829/sql-express-connection-string-mdf-file-location-relative-to-application-locatio">SQL Express connection string: mdf file location relative to application location</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/37689683/connection-string-with-relative-path-to-database-not-working">connection string with relative path to database not working?</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/12932963/how-to-set-a-relative-path-to-a-sql-server-compact-database">How to set a relative path to a SQL Server Compact database?</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/36974326/c-sharp-visual-studio-datadirectory-keyword-in-connection-string-conflicts-md">C# Visual Studio: |DataDirectory| keyword in connection string conflicts mdf files</a></p>
  </li>
  </ul>
  <p>EDIT</p>
  <p>I found a solution on reddit.
  <a href="https://www.reddit.com/r/csharp/comments/k0xaqb/local_db_sql_connection_string_without_using_the/" rel="nofollow noreferrer">https://www.reddit.com/r/csharp/comments/k0xaqb/local_db_sql_connection_string_without_using_the/</a></p>