Getting a list of SQL Server databases on the network

I found this pretty easily with an Internet search.  Copying it here so I can be sure to find it next time I want it.

 Courtesy of Dan Dittenhafer

private static List EnumSqlServers()
{
System.Data.DataTable sqlInstances = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();
List listSqlServers = new List();// Loop to load the unique server names
foreach (System.Data.DataColumn col in sqlInstances.Columns)
{
if (col.ColumnName == “ServerName”)
{
foreach (System.Data.DataRow row in sqlInstances.Rows)
{
if (!listSqlServers.Contains(row[col].ToString()))
{
listSqlServers.Add(row[col].ToString());
}
}
break;
}
}

 

Advertisements

Post a Comment

Required fields are marked *
*
*

%d bloggers like this: