title: Local DB: “A network-related or instance-specific error occurred while establishing a connection to SQL Server.”
body: <p>I have already checked other topics regarding the matter and tried some of the standard solutions but nothing seems to work. My DB is local and I'm trying to connect, it is already created and saved in the folder Helpers with the name of localDB.mdf
   My code is:</p>
  
  <pre><code>public static List&lt;ChatMessages&gt; GetFromDatabase(string query)
      {
          List&lt;ChatMessages&gt; ImportedFiles = new List&lt;ChatMessages&gt;();
          string sql = @"Data Source=|DataDirectory|\Helpers\localDB;AttachDbFilename=\Helpers\localDB.mdf;Integrated Security=True";
          SqlConnection connection = new SqlConnection(sql);
          try
          {
              connection.Open();
              var check = connection.State;
              SqlCommand cmd = new SqlCommand(query, connection);
              var rdr = cmd.ExecuteReader();
              while (rdr.Read())
              {
                  //stuff
              }
              connection.Close();
          }
          catch (Exception e) { MessageBox.Show("error: " + e); }
          var x = ImportedFiles;
          return ImportedFiles;
      }
  </code></pre>
  
  <p>Thanks for your time,
  Best regards</p>