SqlDataSourceEnumerator.GetDataSources Method

Definition

Retrieves a DataTable containing information about all visible SQL Server instances.

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

Returns

A DataTable containing information about the visible SQL Server instances.

Examples

The following console application retrieves information about all the visible SQL Server instances and displays the information in the console window.

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("============================");  
    }  
  }  
}  

Remarks

The table returned by this method contains the following columns, all of which contain strings:

Column Description
ServerName Name of the server.
InstanceName Name of the server instance. Blank if the server is running as the default instance.
IsClustered Indicates whether the server is part of a cluster.
Version Version of the server:

10.0.xx for SQL Server 2008
10.50.x for SQL Server 2008 R2
11.0.xx for SQL Server 2012
12.0.xx for SQL Server 2014
13.0.xx for SQL Server 2016
14.0.xx for SQL Server 2017

Note

Due to the nature of the mechanism used by SqlDataSourceEnumerator to locate data sources on a network, the method will not always return a complete list of the available servers, and the list might not be the same on every call. If you plan to use this function to let users select a server from a list, make sure that you always also supply an option to type in a name that is not in the list, in case the server enumeration does not return all the available servers. In addition, this method may take a significant amount of time to execute, so be careful about calling it when performance is critical.

Applies to

See also