C# – Can’t enumerate SQL Server 2008 Registered Servers with SMO

csmosql server

I had SQL Server 2005 Management Studio installed on my workstation. I have since installed SQL Server 2008 workstation tools and removed the SQL Server 2005 tools. I am now writing a c# program which iterates my registered servers in management studio. Problem is, it is iterating through my old list in the 2005 tools (which have now been uninstalled) and not my 2008 registered servers list.

I thought it might be an assembly references issue, so I check that my SMO assembly references are pointing to the new tools. I am using:

  • Microsoft.SqlServer.ConnectionInfo
  • Microsoft.SqlServer.Management.Sdk.Sfc
  • Microsoft.SqlServer.Smo
  • Microsoft.SqlServer.SqlEnum

I have checked that the assemblies are the 10.0 versions.

My c# code snippet that does the work is:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo.RegisteredServers;

and

    public static void DiscoverServers()
    {
        RegisteredServer[] rsvrs = SqlServerRegistrations.EnumRegisteredServers();
        foreach (RegisteredServer rs in rsvrs)
        {
            Console.WriteLine(rs.Parent.Name + ", " + rs.ServerInstance);

        }
    }

Any ideas on how to fix this? Am I simply using the wrong SMO code?

Cheers,
Mark.

Best Answer

Your code is completely fine. My guess is, that the problem is with your SQL server that is not exposes itself for browsing. Make sure that "SQL Server Browser" Windows Service is running and server browsing is enabled on SQL server configuration.