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;
}
}