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 に答える