title: How to find SQL Server instance name and server name from the code behind using c#?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6824188/sqldatasourceenumerator-instance-getdatasources-does-not-locate-local-sql-serv">SqlDataSourceEnumerator.Instance.GetDataSources() does not locate local SQL server 2008 instance</a>  </p>
  </blockquote>
  
  
  
  <p>I am using code below to get all instance names and server names in my local machine, but it seems it is returning only one instance; how to get all list of instance name and server name in my local machine?</p>
  
  <pre><code>string myServer = Environment.MachineName;
  
  DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
  
  for (int i = 0; i &lt; servers.Rows.Count; i++)
  {
          if (myServer == servers.Rows[i]["ServerName"].ToString()) 
          {
              if ((servers.Rows[i]["InstanceName"] as string) != null)
              {
                  CmbServerName.Visibility = Visibility.Visible;
                  CmbServerName.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
  
              }
              else
              {
                  CmbServerName.Visibility = Visibility.Visible;
                  CmbServerName.Items.Add(servers.Rows[i]["ServerName"]);
  
              }
          }
  }
  </code></pre>
  
  <p>Thanks in advance!</p>