This is a very short code snippet where I will explain how to list the SQL Server instances that are installed on your machine and on the network connected to your machine.
 
 
Namespaces
You will need to import the following namespaces
C#
using System.Data;
using System.Data.Sql;
 
VB.Net
Imports System.Data
Imports System.Data.Sql
 
 
SqlDataSourceEnumerator class
The SqlDataSourceEnumerator class allows you to get the Instances of the SQL Server installed on the machine and also on the network the machine is connected to. You can find more information on the same on the following MSDN article SqlDataSourceEnumerator class.
Now here I’ll get the installed instances of the SQL Server and bind them to a DropDownList Control
C#
DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow dr in dt.Rows)
{
    ddlInstances.Items.Add(string.Concat(dr["ServerName"], "\\", dr["InstanceName"]));
}
 
VB.Net

Dim dt As DataTable = SqlDataSourceEnumerator.Instance.GetDataSources
ForEach dr As DataRow In dt.Rows
     ddlInstances.Items.Add(String.Concat(dr("ServerName"), "\\", dr("InstanceName")))
Next
 
 
Screenshot
List installed SQL Server instances on machine and network using C# and VB.Net
 
 
That’s it in this article. Hope you liked it.