SqlClientFactory.CreateDataSourceEnumerator Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Namespace:   System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

public override DbDataSourceEnumerator CreateDataSourceEnumerator()

Return Value

Type: System.Data.Common.DbDataSourceEnumerator

A new data source enumerator.

The following example displays a list of all available SQL Server data sources, using code that could enumerate data sources for any provider.

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

class Program
    static void Main()
        // List all SQL Server instances:

        Console.WriteLine("Press any key to continue...");
    private static void ListServers(DbProviderFactory factory)
        // This procedure is provider-agnostic, and can list
        // instances of any provider's servers. Of course, 
        // not all providers can create a data source enumerator,
        // so it's best to check the CanCreateDataSourceEnumerator 
        // property before attempting to list the data sources.
        if (factory.CanCreateDataSourceEnumerator)
            DbDataSourceEnumerator instance =
            DataTable table = instance.GetDataSources();

            foreach (DataRow row in table.Rows)
                    row["ServerName"], row["InstanceName"]);

.NET Framework
Available since 2.0
Return to top