asp.net dbproviderfactory(提供程序工厂模型)
来源:互联网 发布:看不到网络上文件共享 编辑:程序博客网 时间:2024/06/09 21:33
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(提供程序工厂模型)
- ASP.NET提供程序模型
- ASP.NET提供程序模型
- 数据提供程序工厂模型
- ASP.NET提供程序
- ASP.NET - 配置文件提供程序
- ASP.NET Forms验证(自定义、角色提供程序)1
- ASP.NET Forms验证(自定义、角色提供程序)2
- ASP.NET Forms验证(自定义、角色提供程序)
- asp.net配置中修改提供程序
- ASP.NET自定义输出缓存提供程序
- ASP.NET 中的 Passport 身份验证提供程序
- ASP.NET Forms验证(自定义、角色提供程序)-.Net编程教程
- Asp.Net页面模型---自定义处理程序
- Asp.Net Forms验证(自定义、角色提供程序、单点登录)
- Asp.Net Forms验证(自定义、角色提供程序、单点登录)
- Asp.Net Forms验证(自定义、角色提供程序)
- (转帖)Asp.Net Forms验证(自定义、角色提供程序)
- IOS atomic与nonatomic,assign,copy与retain的定义和区别
- 国外程序员推荐:每个程序员都应读的书
- Android入门:ListView(SimpleAdapter实现)
- ARM体系结构
- Windows服务程序编写
- asp.net dbproviderfactory(提供程序工厂模型)
- Perl语言学习笔记 12 文件测试
- iOS学习笔记—— Storyboard里面的几种Segue区别:push,modal,popover,replace和custom
- ARM中断
- quick-cocos2dx win 7 编译环境配置及真机调试
- ARM存储管理
- off转obj
- String 转 int
- C++ 字符串转数字 数字转字符串