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

后端 未结 4 2041
别那么骄傲
别那么骄傲 2020-11-27 21:17

I use the following code to list all the remote and local SQL Server instances:

public static void LocateSqlInstances()
  {
     using( DataTable sqlSources          


        
4条回答
  •  被撕碎了的回忆
    2020-11-27 21:44

    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 LocateSqlInstances()
    {
        List results = new List();
    
        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:

    • SqlDataSourceEnumerator.Instance; not returning all instances
    • EnumAvailableSqlServers or SqlDataSourceEnumerator - Incorrect list of available Databases
    • Enumerating SQL Servers
    • Programmatically Listing SQL Servers

提交回复
热议问题