可能重复:
SqlDataSourceEnumerator.Instance.GetDataSources() 未找到本地 SQL Server 2008 实例
我正在使用下面的代码来获取本地机器中的所有实例名称和服务器名称,但它似乎只返回一个实例;如何在我的本地计算机中获取所有实例名称和服务器名称列表?
string myServer = Environment.MachineName;
DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
for (int i = 0; i < servers.Rows.Count; i++)
{
if (myServer == servers.Rows[i]["ServerName"].ToString())
{
if ((servers.Rows[i]["InstanceName"] as string) != null)
{
CmbServerName.Visibility = Visibility.Visible;
CmbServerName.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
}
else
{
CmbServerName.Visibility = Visibility.Visible;
CmbServerName.Items.Add(servers.Rows[i]["ServerName"]);
}
}
}
提前致谢!