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”格式传递提供程序名称和连接字符串来创建
DbProviderFactory
DbConnection 对象。
成功时返回 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参数即可。



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

时间: 2024-07-31 12:51:07

asp.net dbproviderfactory(提供程序工厂模型)的相关文章

Azure Redis 缓存的 ASP.NET 会话状态提供程序

Azure Redis Cache 提供了一个会话状态提供程序,你可以使用其在缓存中(而不是内存中或在 SQL Server 数据库中)存储会话状态.要使用缓存会话状态提供程序,先首先配置缓存,然后使用 Redis 缓存会话状态 NuGet 包配置用于缓存的 ASP.NET 应用程序. 在缓存中存储 ASP.NET 会话状态 若要使用 Redis Cache Session State NuGet 程序包配置 Visual Studio 中的应用程序,请右键单击解决方案资源管理器中的项目,然后选

asp.net采用OLEDB方式导入Excel数据时提示:未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0" 提供程序"

asp.net采用OLEDB方式导入Excel数据时提示:未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0" 提供程序" 笔者在项目中做做了一个从Excel表格中导入数据的模块.大体上asp.net项目中导入Excel大体分成三类: 1)采用c#内置方案System.Data.OleDb(限制较小, 通用) 2)采用Excel的COM组件(会有版本问题) 3)采用伪Excel文件.即使用文本流的方式根据需求自己定义数据格式.同时在服务端进行反格式化 笔者采

自定义ASP.NET Identity(三)- 实现自定义MySQL ASP.NET Identity存储提供程序

ASP.NET Identity是一个可扩展的系统,你可以创建属于你自己的存储提供程序并且将它集成到你的应用中,而不需要重构你的应用.本章将介绍如何创建一个ASP.NET Identity的MySQL存储提供程序.关于如何创建自定义存储提供程序的概述请查看自定义ASP.NET Identity(一)- 自定义ASP.NET Identity存储提供程序.要完成这个指南,你必须安装Visual Studio 2013 Update 2. 这个指南将包含如下内容: 怎样在Azure上创建MySQL数

asp.net 网站部署出错-未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序

错误信息:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 解决方法之一: 编译项目指定目标平台为x86就完了,不能用any cpu 生成->配置管理器->平台->点击Any Cpu选项卡->新建->新建平台->X86 解决方法之二: 在对应的 IIS 应用程序池中,“设置应用程序池默认属性”/“常规”/”启用32位应用程序”,设置为 true. asp.net 网站部署出错-未在本地计算机上注册"Microsoft.Jet.OL

asp.net导出dbf报错“未在本地计算机上注册“VFPOLEDB”提供程序。”

导出dbf文件报错,提示“未在本地计算机上注册“VFPOLEDB”提供程序.” 可以尝试一下方法: 方法一:下载VFPOLEDBSetup.msi 安装 如果方法一不行:继续方法二:下载vfp9.0  最后有链接 若还不行,尝试方法三:打开IIS管理器--找到网站对应的程序池--右键高级设置--启动32位应用程序 设置为true 如图: VFPOLEDBSetup.msi  下载链接:http://pan.baidu.com/s/1qYxLRvm

asp.net运行时错误:没有为扩展名".cshtml"注册的提供程序。

解决方法: 一. 在machine.config或web.config中的<compilation><buildProviders>节注册一个.请确保所注册的提供程序具有包含值“web“或”all“的BuildProviderAppliesToAttribute特性 在machine.config或web.config中修改原来的<compilation> <compilation debug="true" targetFramework=&q

[渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序读取相关数据

这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第六篇:为ASP.NET MVC应用程序读取相关数据 原文:Reading Related Data with the Entity Framework in an ASP.NET MVC Application 译文版权所有,谢绝全文转载--但您可以在您的网站上添加到该教程的链接. 在之前的教程中您已经完成了学校数据模型.在本教程中你将

[渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序使用高级功能

这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第十二篇:为ASP.NET MVC应用程序使用高级功能 原文:Advanced Entity Framework 6 Scenarios for an MVC 5 Web Application 译文版权所有,谢绝全文转载--但您可以在您的网站上添加到该教程的链接. 在之前的教程中,您已经实现了继承.本教程引入了当你在使用实体框架Code

[渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序更新相关数据

这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第六篇:为ASP.NET MVC应用程序更新相关数据 原文: Updating Related Data with the Entity Framework in an ASP.NET MVC Application 译文版权所有,谢绝全文转载--但您可以在您的网站上添加到该教程的链接. 在之前的教程中您已经成功显示了相关数据.在本教程中