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. By using SqlDataSourceEnumerator and EnumAvailableSqlServers, we can get SQL Server instances that are installed on your machine as well as on the network, connected.
For this, we need to import these classes.
- using System;
- using System.Data;
- using System.Data.Sql;
- using Microsoft.Win32;
- using Microsoft.SqlServer.Management.Smo;
Using 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.
- private void GetDataSources()
- {
- SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
- DataTable table = instance.GetDataSources();
- string ServerName = Environment.MachineName;
- foreach (DataRow row in table.Rows)
- {
- Console.WriteLine (ServerName + "\\" + row["InstanceName"].ToString());
- }
- }
Using EnumAvailableSqlServers Class
First of all, you need to add a reference to the Microsoft.SqlServer.smo.dll file to your project which is available in NuGet Package Manager.
- private void GetDataSources()
- {
- DataTable table = SmoApplication.EnumAvailableSqlServers(true);
- string ServerName = Environment.MachineName;
- foreach (DataRow row in table.Rows)
- {
- Console.WriteLine(ServerName + "\\" + row["InstanceName"].ToString());
- }
- }
EnumAvailableSqlServers and SqlDataSourceEnumerator will only find named instances if the SQL Server Browser services are running. If you want to get the SQL Server instance name on the current computer, you can read the information from the registry.
- private void GetDataSources2()
- {
- string ServerName = Environment.MachineName;
- RegistryView registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
- using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
- {
- RegistryKey instanceKey = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
- if (instanceKey != null)
- {
- foreach (var instanceName in instanceKey.GetValueNames())
- {
- Console.WriteLine(ServerName + "\\" + instanceName);
- }
- }
- }
- }
These simple tips can help all readers. I hope you find this blog helpful.
Happy Coding!

acheampong kingsfordPosted Apr 3, 2022, 5:06 PM
You are too much
Stewart GordonPosted Feb 17, 2021, 6:02 PM
Thank you. But one thing isn't clear to me. If the machine has an SQL Server instance that is just named as the machine name with no second part - MYPC rather than MYPC\SQLSERVER - then will this show in the output? How will it appear? Or do I need different code to pick up such a thing?
Nikos Spanos, MScPosted Sep 27, 2020, 11:00 AM
Hey Amit. Nice article. I assume that you propose three different ways to extract the Server Name of a local machine. For a reason, only the third method "GetDataSources2()" worked for me.
Olaf BarteltPosted Nov 6, 2019, 4:17 AM
Thanks for the code. But isn't using Environment.MachineName in GetDataSources wrong for SQL Servers on the network (i.e. not local)?
UdayTeja GPosted Jul 10, 2019, 12:59 AM
Using SqlDataSourceEnumerator Class not returning me SQL Servers instances with in the machine.
Laxmidhar SahooPosted May 6, 2019, 12:20 PM
Thanks for a good naration