title: Get instance name of local sql server
body: <p>I'm totally confused of how to get the local sql server instance. The only thing I receive is the Server names. Here are the different approaches I have tried:</p>
  
  <pre><code>DataTable availableServers = SmoApplication.EnumAvailableSqlServers([All of the 3 inputs]);
  foreach (DataRow serverRow in availableServers.Rows)
  {
       // foreach row list "Name", "Server", "Instance" etc...
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>SqlDataSourceEnumerator sdse = SqlDataSourceEnumerator.Instance;
  DataTable table = sdse.GetDataSources();
  foreach (DataRow row in table.Rows)
  {
       // foreach row list "ServerName", "InstanceName", "IsClustered" and "Version"
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>ManagedComputer mc = new ManagedComputer();
  ServerInstance instance = mc.ServerInstances;
  </code></pre>
  
  <p>Is there some other way I can get the local sql server instance name? </p>
  
  <p>I know that when connecting to SQL server in management studio the connect dialog displays my local instance as .\sqlexpress or (local)\sqlexpress. Also when deploying a database installation in InstallShield the InstallShield "locate sqlserver"-dialog gets the correct instance for my local host. What are they doing correct?</p>