title: c# 2008 SQL Server Express Connection String
body: <p>I have a 2008 SQL Server Express installed on one of my machines and I'm attempting to establish a remote connection... when I use the MS SQL Server Management Studio I can log into the database without any problems at all (with the same credentials), but when I try to create a connection string in my C# application I get an exception:</p>
  
  <blockquote>
    <p>A network-related or instance-specific
    error occurred while establishing a
    connection to SQL Server.  The server
    was not found or was not accessible. 
    Verify that the instance name is
    correct and that SQL Server is
    configured to allow remote
    connections.</p>
  </blockquote>
  
  <p>Here is what my connection string looks like (the private information is changed):</p>
  
  <pre><code>"Data Source="MACHINENAME\\SQLEXPRESS";User ID="Admin";Password="the_password";Initial Catalog="MyDatabase";Integrated Security=True;Connect Timeout=120");
  </code></pre>
  
  <p>As I said, <strong>I can login using the Management Studio with the same settings</strong>: same user id, password and data source name, but it fails when I attempt to open a connection with the above connection string. </p>
  
  <p>Note:</p>
  
  <ol>
  <li><p>I have enabled the remote connectivity on the server, disabled the firewall, enabled TCP/IP connection to the server, turned on the SQL Browser.</p></li>
  <li><p>The connection string works fine when I'm on the same machine.</p></li>
  <li><p>I looked up the Integrated Security option and I set it to false just to make sure that it's not attempting to use the Windows Login, but it still fails.</p></li>
  <li><p>The database is setup to allow both windows login and database login.</p></li>
  <li><p>I changing the Integrated Security option to SSPI, True, and finally False, all 3 gave me the same error as above.</p></li>
  </ol>
  
  <p>Can anybody tell me if I'm doing something wrong?</p>
  
  <p>UPDATE, here is my exact code (this time only the password is removed, and I've added a picture of management studio running on the same machine):</p>
  
  <pre><code>string _connectionString =
              //string.Format("Server=%s;User ID=%s;Password=%s;Database=%s;Connect Timeout=120", // Same problem
              //string.Format("Server=%s;User ID=%s;Password=%s;Database=%s;Integrated Security=False;Connect Timeout=120", // Same problem
              string.Format("Data Source=%s;User ID=%s;Password=%s;Initial Catalog=%s;Integrated Security=False;Connect Timeout=120", // Same problem
              "GANTCHEVI\\SQLEXPRESS",
              "FinchAdmin",
              "the_password",
              "Finch");
  </code></pre>
  
  <p><a href="http://s113.photobucket.com/albums/n202/ozpowermo/?action=view&amp;current=ManagementStudio.jpg" rel="noreferrer">Connected Via Management Studio: See Picture http://s113.photobucket.com/albums/n202/ozpowermo/?action=view&amp;current=ManagementStudio.jpg</a></p>
  
  <p><a href="http://s113.photobucket.com/albums/n202/ozpowermo/?action=view&amp;current=ManagementStudio.jpg" rel="noreferrer">http://s113.photobucket.com/albums/n202/ozpowermo/?action=view&amp;current=ManagementStudio.jpg</a></p>
  
  <p><strong>I FIGURED IT OUT:</strong></p>
  
  <p>When using the "Data Source=" label one should use the "User Id", if you use User ID it doesn't seem like it works!</p>
  
  <pre><code>string _connectionString = "Data Source=GANTCHEVI\\SQLEXPRESS;Initial Catalog=Finch;Integrated Security=False;User Id=FinchAdmin;Password=the_password;Connect Timeout=0";"
  </code></pre>