tags:

views:

62

answers:

1

I am trying to use SMO (VS 2010, SQL Server 2008) to connect to SQL Server and view the server protocol configuration. I can connect and list the Services and ClientProtocols as well as the account MSSQLSERVER service is running under. However, the ServerInstances collection is empty. The only instance on the target server is the default (MSSQLSERVER), shouldn't that be in the collection? How can I get an instance of it so I can inspect the ServerProtocols collection? Here's the code I'm using:

    class Program
{
    static void Main(string[] args)
    {
        //machine hosting installed sql server instance
        ManagedComputer host = new ManagedComputer("fully-qualified.host.name");


        if (host.ServerInstances.Count != 0)
        {
            //why is this 0? Is it because only the DEFAULT instance exists?
            Console.WriteLine("/////////////// INSTANCES ////////////////");
            foreach (ServerInstance inst in host.ServerInstances)
            {
                Console.WriteLine(inst.Name);
            }
        }

        Console.WriteLine("/////////////// SERVICES ////////////////");
        // enumerate sql services (looking for MSSSQLSERVER)
        foreach (Service svc in host.Services)
        {
            Console.WriteLine(svc.Name);
        }

        Console.WriteLine("/////////////// DETAILS ////////////////");

        // get name of MSSQLSERVER instance from user (pick from list above)
        Service mssqlserver = host.Services["MSSQLSERVER"];

        // print service account: .\{account} == "local account", "LocalSystem", "NetworkService", {domain}\{account} == "domain account"
        Console.WriteLine("Service Account: {0}", mssqlserver.ServiceAccount);

        // get client protocols
        foreach (ClientProtocol cp in host.ClientProtocols)
        {
            Console.WriteLine("{0} {1} ({2})", cp.Order, cp.DisplayName, cp.IsEnabled ? "Enabled" : "Disabled");
        }

    }
}

I've also tried:

            Urn u = new Urn("ManagedComputer[@Name='fully-qualified.host.name']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']");
        ServerProtocol tcp = host.GetSmoObject(u) as ServerProtocol;
        if (tcp != null)
        {
            Console.WriteLine("{0}", tcp.DisplayName);
        }

But I get an error message stating: "child expressions are not supported." Any ideas what's wrong?

A: 

So I never found the answer to this, but I came up with a workaround.

            ManagedComputer host = new ManagedComputer(hostName);

        Server server = new Server();
        Console.WriteLine("TcpEnabled: {0}", server.TcpEnabled);

This gets me what I need, I would prefer to be able to enumerate the ServerProtocols, but this will allow me to verify the very minimum.

Mark J Miller