Listing Available SQL Servers Using C#

Introduction

This blogs explains how to list available SQL Servers using ADO.net in C#, using GetDataSources() function of SqlDataSourceEnumeratorclass it will be possible to list available servers. the SqlDataSourceEnumeratorclass Comes under using System.Data.Sql namespace. the GetDataSource function fetches list of servers in Datatable.

DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();

foreach (DataRow Row in servers.Rows)

{

     Console.WriteLine("Server Name" + "        " + "Instance Name" + "        " + "Version No");

     Console.WriteLine(Row[0].ToString() + "        " + Row[1].ToString() + "        " + Row[3].ToString());

}

 
In above code I assigned the returned datatable by GetDataSources to servers, which is object of System.Data.DataTable class. using foreach loop i have extracted server details such as Name, instance name and version