gpt4 book ai didi

c# - 如何使用c#从后面的代码中查找SQL Server实例名称和服务器名称?

转载 作者:行者123 更新时间:2023-11-30 13:59:32 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
SqlDataSourceEnumerator.Instance.GetDataSources() does not locate local SQL server 2008 instance

我正在使用下面的代码获取本地机器中的所有实例名称和服务器名称,但它似乎只返回一个实例;如何在我的本地计算机中获取所有实例名称和服务器名称的列表?

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"]);

}
}
}

提前致谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com