List SQL Server Instances in C#

When building administrative tools, installers, or database-driven applications, you may need to detect available SQL Server instances on a machine or across a network. In C#, this can be done easily using built-in .NET libraries.

This post explains:
  • What SQL Server instances are
  • How to list them in C#
  • Example code

List SQL Server Instances in C# (Step-by-Step Guide)

Getting Started

When building a .NET application that connects to SQL Server, one common requirement is allowing users to select or detect available SQL Server instances. This is especially important for installer tools, configuration utilities, ERP systems, and desktop applications.

This guide explains how to list SQL Server instances in .NET along with best practices used in professional software installers.

What Is a SQL Server Instance?

An instance is a separate installation of Microsoft SQL Server running on a machine.

There are two main types:
  • Default instanceMACHINE_NAME
  • Named instanceMACHINE_NAME\INSTANCE_NAME
A single machine can host multiple SQL Server instances.

Listing SQL Server Database Instances in C#

There are three approaches to list SQL Server instances, which are listed below.

  1. Using SqlDataSourceEnumerator (.NET Framework)
  2. Using SMO (SQL Server Management Objects)
  3. Using Hybrid Approach (Reliable)

Method 1: Using SqlDataSourceEnumerator (Recommended)

SqlDataSourceEnumerator is a class in .NET Framework used to discover available SQL Server instances on a network. It belongs to the namespace System.Data.Sql and is typically used when you want your application to automatically list SQL Server instances instead of manually typing server names.

Example (C#)
using System;
using System.Data;
using System.Data.Sql;
class Program
{
  static void Main()
  {
    SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
    DataTable table = instance.GetDataSources();
    foreach (DataRow row in table.Rows)
    {
      Console.WriteLine("Server Name: " + row["ServerName"]);
      Console.WriteLine("Instance Name: " + row["InstanceName"]);
      Console.WriteLine("Is Clustered: " + row["IsClustered"]);
      Console.WriteLine("Version: " + row["Version"]);
      Console.WriteLine("-----------------------------------");
    }
    Console.ReadLine();
  }
}
Output Example
Server Name: MYPC
Instance Name: SQLEXPRESS
Is Clustered: No
Version: 16.0.1000.6
How It Works
The class scans the network and returns a DataTable containing information about SQL Server instances. Typical columns returned include:
  • ServerName
  • InstanceName
  • IsClustered
  • Version

Important Notes
  • SQL Browser Service Must Be Running: The SQL Server Browser service must be enabled for instance discovery.
  • Network Discovery May Be Blocked: Firewalls may block UDP port 1434, which SQL Server uses for discovery.
  • It Only Shows Visible Instances: Hidden or firewalled instances will not appear.

Method 2: Using SMO (SQL Server Management Objects)

SMO (SQL Server Management Objects) is a .NET library used to manage and automate SQL Server programmatically. It allows developers to control and administer SQL Server using code instead of manually using tools like SQL Server Management Studio. SMO works with Microsoft SQL Server and is part of the Microsoft data management ecosystem.

Install Required NuGet Packages

  dotnet add package Microsoft.SqlServer.SqlManagementObjects
  
SMO depends on the SQL client:

  dotnet add package Microsoft.Data.SqlClient
  
Example

To list SQL Server instances using SMO in .Net Framework or modern .NET (like .NET 8 or above), you typically use the SmoApplication.EnumAvailableSqlServers() method from SQL Server Management Objects.

using System;
using System.Data;
using Microsoft.SqlServer.Management.Smo;

class Program
{
    static void Main()
    {
        DataTable servers = SmoApplication.EnumAvailableSqlServers(true);

        foreach (DataRow row in servers.Rows)
        {
            Console.WriteLine("Server Name: " + row["Name"]);
        }
    }
}
Example Output
Server Name: DESKTOP-PC
Server Name: DESKTOP-PC\SQLEXPRESS
Server Name: DB-SERVER01

This is similar to how SQL Server Management Studio finds SQL Server instances on a network. To list local instances only pass 'false' as parameter like this SmoApplication.EnumAvailableSqlServers(false);

Method 3: Using Hybrid Approach (Reliable)

To detect local SQL Server instances, use Windows Registry to find SQL Server instances installed locally.

Registry location:

  
  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL
  
Example code:
using Microsoft.Win32;
var key = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL");
foreach (var instance in key.GetValueNames())
{
  Console.WriteLine(Environment.MachineName + "\\" + instance);
}

Summary

Listing SQL Server instances in .NET application can be achieved using SqlDataSourceEnumerator and SQL Server Management Objects (SMO) combined with a hybrid discovery strategy.

Thanks

Kailash Chandra Behera

I am an IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال