Question

I have a piece of code that is supposed to return all instances of SQL Server installed on my machine. It looks like this:

DataTable dtLocalServers = SmoApplication.EnumAvailableSqlServers(true);

foreach (DataRow dServer in dtLocalServers.Rows)
{
   Console.WriteLine(dServer.ToString());
}

It works, compiles, and runs. The resulting datatable is not very useful, however. It looks like this:

Name    |  Server   |   Instance   |   IsClustered | Version   |   IsLocal
IAN-PC  |  IAN-PC   |              |   True        |           |   True

The problem is that although this returns a result, this doesn't give me any information about the server. Particularly, I'm trying to get the version (SQL Server 2008 Express R2 or SQL Server Express 2012), which I'm assuming should be in the 'Version' field. I know for a fact that I have two copies running locally for the sake of this test.

Is there anything else I can do to get better results?

EDIT: IAN-PC is the name of my computer. There are two running instances of SQL Server on my computer, IANSQLEXP (SQL Server 2008 Express R2), and SQLEXPRESS (SQL Server Express 2012). In the SQL Server Configuration Manager, there are also two Agents that are not running and a SQL Server Browser that is not running.

Was it helpful?

Solution 2

The problem here is that the SQL Server Browser was deactivated on my computer. The post that I found the answer in was here. Another helpful link on from MSDN that helped is here. Thanks for the help, guys!

OTHER TIPS

You can enumerate the servers and turn them into a List<Server>.

var servers =
    SmoApplication
        .EnumAvailableSqlServers()
        .AsEnumerable()
        .Select(s => new Server(s[0].ToString())) //Server name is first column
        .ToList();

You can use the Version property on Server to get the server version.

var unavailableServers= new List<Server>();
var activeServers = new List<Server>();
var tasks = new List<Task>();

foreach(var server in servers)
{
    var task =
        Task.Run(() =>
            {
                try
                {

                    server.ConnectionContext.Connect();
                    server.ConnectionContext.Disconnect();
                    activeServers.Add(server);
                }
                catch(ConnectionFailureException)
                {
                    unavailableServers.Add(server);
                }
            });

    tasks.Add(task);
}

Task.WaitAll(tasks.ToArray());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top