13

我创建了一个桌面应用程序。在应用程序启动时,我想显示本地 PC 上所有可用 SQL Server 实例的列表,并允许选择要连接的 SQL Server 名称。

无论如何要获取本地 PC 上可用的所有 SQL Server 实例名称的列表吗?

非常感谢。

4

3 回答 3

18
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"].ToString());
      }
  }
于 2012-05-28T08:11:13.213 回答
4
        //// Retrieve the enumerator instance, and then retrieve the data sources.
        SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
        DataTable dtDatabaseSources = instance.GetDataSources();

        //// Populate the data sources into DropDownList.            
        foreach (DataRow row in dtDatabaseSources.Rows)
            if (!string.IsNullOrWhiteSpace(row["InstanceName"].ToString()))
                Model.DatabaseDataSourceNameList.Add(new ExportWizardChooseDestinationModel
                {
                    DatabaseDataSourceListItem = row["ServerName"].ToString()
                        + "\\" + row["InstanceName"].ToString()
                });
于 2014-03-18T06:15:54.357 回答
2

尝试

SqlDataSourceEnumerator.Instance.GetDataSources()
于 2012-05-28T08:11:47.690 回答