SqlClientFactory.CanCreateDataSourceEnumerator 属性

定义

获取一个指示是否可创建 SqlDataSourceEnumerator 的值。

public:
 virtual property bool CanCreateDataSourceEnumerator { bool get(); };
public override bool CanCreateDataSourceEnumerator { get; }
member this.CanCreateDataSourceEnumerator : bool
Public Overrides ReadOnly Property CanCreateDataSourceEnumerator As Boolean

属性值

true 如果可以创建 , SqlDataSourceEnumerator 则为 ;否则为false .

示例

下面的示例使用可枚举任何提供程序的数据源的代码显示所有可用SQL Server数据源的列表。

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

class Program
{
    static void Main()
    {
        // List all SQL Server instances:
        ListServers(SqlClientFactory.Instance);

        Console.WriteLine();
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
    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 =
                factory.CreateDataSourceEnumerator();
            DataTable table = instance.GetDataSources();

            foreach (DataRow row in table.Rows)
            {
                Console.WriteLine("{0}\\{1}",
                    row["ServerName"], row["InstanceName"]);
            }
        }
    }
}

注解

DbProviderFactory 提供 CanCreateDataSourceEnumerator 属性,以便继承器可以指示它们是否可以提供数据源枚举器。 显示 SqlClientFactory 此属性,但其值始终 true为 。

适用于