C# – Get SQL Server Instances
by admin 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);
}
// 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); }
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 | // 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); } |