SQL Server 2016 list instances

1k views Asked by At

I am using the code below in my windows application to get local servers, when I was using SQL Server 2012 it was working without any errors, but when I downloaded SQL Server 2016, I got the exception :

Exception: An exception occurred in SMO while trying to manage a service. Inner Exception: Failed to retrieve data for this request.

The Code:

public List<string> findLocalServers()
{
    var servers = new List<string>();
    try
    {
        var serverCollection = new ManagedComputer().ServerInstances.Cast<ServerInstance>().Select(instance => String.IsNullOrEmpty(instance.Name) ?
                                        instance.Parent.Name : instance.Parent.Name)
                                    .ToArray();

        foreach (var server in serverCollection.Where(server => !servers.Contains(server)))
        {
            servers.Add(server);
        }

        return servers;
    }
    catch (Exception ex)
    {            
        return null;
    }
}
1

There are 1 answers

0
user7519831 On

I had same problem. These steps helped me.

Reference to Microsoft.SqlServer.ConnectionInfo increased to version 13.x.x.x (via Add reference -> and find upper version in Extensions). Same step for Microsoft.SqlServer.Management.Sdk.Sfc and Microsoft.SqlServer.Smo.

I did not find increased version for Microsoft.SqlServer.SqlWmiManagement. So I removed reference to this assembly and via browse I founded C:\Program Files\Microsoft SQL Server\130\SDK\Assemblies\Microsoft.SqlServer.SqlWmiManagement.dll.

That´s all.