且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何开始使用C#代码可用的SQL服务器列表?

更新时间:2023-02-07 20:54:34

 字符串myServer上= Environment.MachineName; 

DataTable的服务器= SqlDataSourceEnumerator.Instance.GetDataSources();
的for(int i = 0; I< servers.Rows.Count;我++)
{
如果(myServer上== servers.Rows [I] [服务器名称]的ToString(。 ))/////用于获取本地计算机////
{
服务器IF((servers.Rows [I] [实例名]作为字符串)!= NULL)
CmbServerName.Items.Add(servers.Rows [I] [服务器名称] +\\+ servers.Rows [I] [实例名]);
,否则
CmbServerName.Items.Add(servers.Rows [I] [服务器名称]);
}
}


I have created a desktop application. On application launch I want to display the list of all available SQL Server instances on the local PC, and allow to choose a SQL Server name to connect with.

Is there anyway to get the list of all SQL Server instance names that are available on the local PC?

Thanks a lot.

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()) ///// used to get the servers in the local machine////
     {
         if ((servers.Rows[i]["InstanceName"] as string) != null)
            CmbServerName.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
         else
            CmbServerName.Items.Add(servers.Rows[i]["ServerName"]);
      }
  }