获取局域网内所有(数据库)SQLServer机器名和实例名
public List<string> GetDataSource()
{
DataTable dataSources = SqlClientFactory.Instance.CreateDataSourceEnumerator().GetDataSources();
DataColumn column = dataSources.Columns["InstanceName"];
DataColumn column2 = dataSources.Columns["ServerName"];
DataRowCollection rows = dataSources.Rows;
List<string> Serverlist = new List<string>();
string array = string.Empty;
for (int i = 0; i < rows.Count; i++)
{
string str2 = rows[i][column2] as string;
string str = rows[i][column] as string;
if (((str == null) || (str.Length == 0)) || ("MSSQLSERVER" == str))
{
array = str2;
Console.WriteLine(array);
}
else
{
array = str2 + @"\" + str;
Console.WriteLine(array);
}
Serverlist.Add(array);
}
Serverlist.Sort();
return Serverlist;
}