asp.net dbproviderfactory(提供程序工厂模型)

DbProviderFactories该类有几个静态方法

SQL Server提供程序工厂对象的方法

DbProviderFactory fact=DbProviderFactories.GetFactory("System.Data.Client");

GetFactory 方法接收一个字符串,该字符串代表提供程序的名称。这个名称位于machine.config文件中,他会对所有已注册的提供程序进行枚举,返回与该名称匹配的程序集和类名信息。工厂类并不会直接被实例化(即所谓的单例模式)。一旦获得工厂对象,就可以调用如下方法

CreateCommand 返回代表提供程序特定的命令对象

CreateCommandBuilder 返回提供程序特定的命令生成器对象

CrateConnection 返回提供程序特定的连接对象

CreateDataAdapter 返回提供程序特定的数据适配器对象

CreateParameter 返回提供程序特定的参数对象


若要创建提供程序工厂,必须提供连接字符串和提供程序名称。此示例演示如何通过以“System.Data.ProviderName”固定格式传递提供程序名称来从应用程序配置文件中检索连接字符串。代码循环访问 ConnectionStringSettingsCollection成功时代码返回 ProviderName;否则返回 null。如果提供程序有多项,则返回找到的第一项。

按提供程序名称检索连接字符串

// Retrieve a connection string by specifying the providerName.
// Assumes one connection string per provider in the config file.
static string GetConnectionStringByProvider(string providerName)
{
    // Return null on failure.
    string returnValue = null;

    // Get the collection of connection strings.
    ConnectionStringSettingsCollection settings =
        ConfigurationManager.ConnectionStrings;

    // Walk through the collection and return the first 
    // connection string matching the providerName.
    if (settings != null)
    {
        foreach (ConnectionStringSettings cs in settings)
        {
            if (cs.ProviderName == providerName)
                returnValue = cs.ConnectionString;
            break;
        }
    }
    return returnValue;
}

创建 DbProviderFactory 和 DbConnection

示例演示如何通过以“System.Data.ProviderName”格式传递提供程序名称和连接字符串来创建 DbProviderFactoryDbConnection 对象。 成功时返回 DbConnection 对象;出错时返回 null(在 Visual Basic 中为 Nothing)。

代码通过调用 GetFactory 获取 DbProviderFactory然后,CreateConnection 方法创建 DbConnection 对象并将 ConnectionString 属性设置为连接字符串。、

// Given a provider name and connection string, 
// create the DbProviderFactory and DbConnection.
// Returns a DbConnection on success; null on failure.
static DbConnection CreateDbConnection(
    string providerName, string connectionString)
{
    // Assume failure.
    DbConnection connection = null;

    // Create the DbProviderFactory and DbConnection.
    if (connectionString != null)
    {
        try
        {
            DbProviderFactory factory =
                DbProviderFactories.GetFactory(providerName);

            connection = factory.CreateConnection();
            connection.ConnectionString = connectionString;
        }
        catch (Exception ex)
        {
            // Set the connection to null if it was created.
            if (connection != null)
            {
                connection = null;
            }
            Console.WriteLine(ex.Message);
        }
    }
    // Return the connection.
    return connection;
}

这样如果我们要更改数据库只需在配置文件中更改相应的连接字符串,以及更改CreateDbConnection的providerName参数即可。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值