How to Get Server name and instance name in c# and show in combobox

If you are interested in Sql Server you can use something like this:

using System.Data;
using System.Data.Sql;

var instances = SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow instance in instances.AsEnumerable())
{
    Console.WriteLine($"ServerName: {instance["ServerName"]}; "+
       " Instance: {instance["InstanceName"]}");
}

More information about the SqlDataSourceEnumerator class you can find on MSDN.

Note:

This class will look into the local network for servers, if your network is large then there might be a delay in acquiring the response. Also for the empty string instance name, that should be the default instance for that SQL Server.

You can get this information using the SMO too, if you want.

Leave a Comment