SqlDataSourceEnumerator.GetDataSources 方法

定义

检索包含有关所有可见 SQL Server 实例的信息的 DataTable

public:
 override System::Data::DataTable ^ GetDataSources();
public override System.Data.DataTable GetDataSources ();
override this.GetDataSources : unit -> System.Data.DataTable
Public Overrides Function GetDataSources () As DataTable

返回

一个 DataTable,包含有关可见 SQL Server 实例的信息。

示例

以下控制台应用程序检索有关所有可见SQL Server实例的信息,并在控制台窗口中显示信息。

Imports System.Data.Sql  

Module Module1  
  Sub Main()  
    ' Retrieve the enumerator instance and then the data.  
    Dim instance As SqlDataSourceEnumerator = _  
     SqlDataSourceEnumerator.Instance  
    Dim table As System.Data.DataTable = instance.GetDataSources()  

    ' Display the contents of the table.  
    DisplayData(table)  

    Console.WriteLine("Press any key to continue.")  
    Console.ReadKey()  
  End Sub  

  Private Sub DisplayData(ByVal table As DataTable)  
    For Each row As DataRow In table.Rows  
      For Each col As DataColumn In table.Columns  
        Console.WriteLine("{0} = {1}", col.ColumnName, row(col))  
      Next  
      Console.WriteLine("============================")  
    Next  
  End Sub  
End Module  
using System.Data.Sql;  

class Program  
{  
  static void Main()  
  {  
    // Retrieve the enumerator instance and then the data.  
    SqlDataSourceEnumerator instance =  
      SqlDataSourceEnumerator.Instance;  
    System.Data.DataTable table = instance.GetDataSources();  

    // Display the contents of the table.  
    DisplayData(table);  

    Console.WriteLine("Press any key to continue.");  
    Console.ReadKey();  
  }  

  private static void DisplayData(System.Data.DataTable table)  
  {  
    foreach (System.Data.DataRow row in table.Rows)  
    {  
      foreach (System.Data.DataColumn col in table.Columns)  
      {  
        Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);  
      }  
      Console.WriteLine("============================");  
    }  
  }  
}  

注解

此方法返回的表包含以下列,其中所有列都包含字符串:

描述
ServerName 服务器的名称。
InstanceName 服务器实例的名称。 如果服务器作为默认实例运行,则为空。
IsClustered 指明服务器是否属于群集。
版本 服务器版本:

10.0.xx for SQL Server 2008
10.50.x(适用于 SQL Server 2008 R2)
2012 SQL Server 11.0.xx
2014 SQL Server 12.0.xx
13.0.xx for SQL Server 2016
14.0.xx for SQL Server 2017

注意

由于 用于在网络上查找数据源的机制 SqlDataSourceEnumerator 的性质,方法不会始终返回可用服务器的完整列表,并且每次调用的列表可能都不相同。 如果计划使用此函数让用户从列表中选择服务器,请确保始终提供一个选项来键入不在列表中的名称,以防服务器枚举未返回所有可用服务器。 此外,此方法的执行可能需要大量时间,因此在性能至关重要时请小心调用它。

适用于

另请参阅