beta.blog

C# – Get SQL Server Instances

by on Jun.08, 2012, under Programming

The following example code will demonstrate a way of getting a list of all available SQL server instances.

// Retrieve the enumerator instance and then the data.
System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance;
System.Data.DataTable table = instance.GetDataSources();

// We'll store a list of all available instances
List<string> instances = new List<string>();
string cur = "";

foreach (System.Data.DataRow row in table.Rows)
{
    cur = row.ItemArray[1].ToString();
    // I'm not interested in these instances. Ignore them.
    if (cur == "SQLEXPRESS" || cur == "TRAFFIC" || cur == "DRS" || cur == "HELLA" || cur == "SQL2008R2" || cur == "FTVIEWX64TAGDB")
        continue;
    // Add the remaining instances to my list
    if (instances.Contains(cur) == false)
        instances.Add(cur);
}

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!