SqlDataSourceEnumerator.Instance.GetDataSources() does not locate local SQL server 2008 instance

You are skipping over servers that are not named instances. Modify your code:

public class SqlServerInstance
{
    public string ServerInstance { get; set; }
    public string Version { get; set; } 
}

public static List<SqlServerInstance> LocateSqlInstances()
{
    List<SqlServerInstance> results = new List<SqlServerInstance>();

    using (DataTable sqlSources = SqlDataSourceEnumerator.Instance.GetDataSources())
    {
        foreach (DataRow source in sqlSources.Rows)
        {
            string servername;
            string instancename = source["InstanceName"].ToString();

            if (!string.IsNullOrEmpty(instancename))
            {
                servername =  source["ServerName"].ToString() + '\\' + instancename;
            }
            else
            {
                servername = source["ServerName"].ToString();
            }

            results.Add(new SqlServerInstance (){ ServerInstance = servername, Version = source["Version"].ToString() });
        }
    }

    return results;
}

Please Note: SqlDataSourceEnumerator.Instance.GetDataSources() has drawbacks:

  • Subject to firewall rules (Blocked TCP/IP 1433 and UDP 1434)
  • Doesn’t find SQL Servers if the SQL Browser is off
  • Doesn’t find SQL Servers if they are hidden
  • List contents not guaranteed to be repeatable (due to timeouts). In fact, a subsequent call is quite likely to give a different list depending on the network I/O, server performance, number of servers on the network and other time-dependent constraints

Several sources say you have to make 2 calls to SqlDataSourceEnumerator.Instance.GetDataSources()

Refs:

Leave a Comment