使用反射+抽象工厂的数据访问(3)
2010-12-31 00:22
232 查看
最后实现的代码如下:
所有数据库的基类(DbProviderFactory.cs)
using System;
using System.Data;
using System.Data.Common;
using System.Reflection;
namespace SplendidCRM
{
public class DbProviderFactory
{
protected string m_sConnectionString ;
protected Assembly m_asmSqlClient ;
protected System.Type m_typSqlConnection ;
protected System.Type m_typSqlCommand ;
protected System.Type m_typSqlDataAdapter ;
protected System.Type m_typSqlParameter ;
protected System.Type m_typSqlBuilder ;
/// <summary>
/// DbProviderFactory构造函数,用一系列参数指定实际使用的是什么数据库
/// </summary>
public DbProviderFactory(string sConnectionString, string sAssemblyName, string sConnectionName, string sCommandName, string sDataAdapterName, string sParameterName, string sBuilderName)
{
m_sConnectionString = sConnectionString;
// 使用反射
m_asmSqlClient = Assembly.LoadWithPartialName(sAssemblyName);
if ( m_asmSqlClient == null ) throw(new Exception("无法载入 " + sAssemblyName));
m_typSqlConnection = m_asmSqlClient.GetType(sConnectionName );
m_typSqlCommand = m_asmSqlClient.GetType(sCommandName);
m_typSqlDataAdapter = m_asmSqlClient.GetType(sDataAdapterName);
m_typSqlParameter = m_asmSqlClient.GetType(sParameterName);
}
/// <summary>
/// 连接对象,此时无论是什么数据库连接,都是IDbConnection的子类,所以返回的类型为IDbConnection
/// </summary>
public IDbConnection CreateConnection()
{
Type[] types = new Type[1];
types[0] = Type.GetType("System.String");
ConstructorInfo info = m_typSqlConnection.GetConstructor(types);
object[] parameters = new object[1];
parameters[0] = m_sConnectionString;
IDbConnection con = info.Invoke(parameters) as IDbConnection;
if ( con == null )
throw(new Exception(" 无法创建连接Connection"));
return con;
}
本文出自 “Apprentice” 博客,请务必保留此出处http://apprentice.blog.51cto.com/2214645/1360590
所有数据库的基类(DbProviderFactory.cs)
using System;
using System.Data;
using System.Data.Common;
using System.Reflection;
namespace SplendidCRM
{
public class DbProviderFactory
{
protected string m_sConnectionString ;
protected Assembly m_asmSqlClient ;
protected System.Type m_typSqlConnection ;
protected System.Type m_typSqlCommand ;
protected System.Type m_typSqlDataAdapter ;
protected System.Type m_typSqlParameter ;
protected System.Type m_typSqlBuilder ;
/// <summary>
/// DbProviderFactory构造函数,用一系列参数指定实际使用的是什么数据库
/// </summary>
public DbProviderFactory(string sConnectionString, string sAssemblyName, string sConnectionName, string sCommandName, string sDataAdapterName, string sParameterName, string sBuilderName)
{
m_sConnectionString = sConnectionString;
// 使用反射
m_asmSqlClient = Assembly.LoadWithPartialName(sAssemblyName);
if ( m_asmSqlClient == null ) throw(new Exception("无法载入 " + sAssemblyName));
m_typSqlConnection = m_asmSqlClient.GetType(sConnectionName );
m_typSqlCommand = m_asmSqlClient.GetType(sCommandName);
m_typSqlDataAdapter = m_asmSqlClient.GetType(sDataAdapterName);
m_typSqlParameter = m_asmSqlClient.GetType(sParameterName);
}
/// <summary>
/// 连接对象,此时无论是什么数据库连接,都是IDbConnection的子类,所以返回的类型为IDbConnection
/// </summary>
public IDbConnection CreateConnection()
{
Type[] types = new Type[1];
types[0] = Type.GetType("System.String");
ConstructorInfo info = m_typSqlConnection.GetConstructor(types);
object[] parameters = new object[1];
parameters[0] = m_sConnectionString;
IDbConnection con = info.Invoke(parameters) as IDbConnection;
if ( con == null )
throw(new Exception(" 无法创建连接Connection"));
return con;
}
本文出自 “Apprentice” 博客,请务必保留此出处http://apprentice.blog.51cto.com/2214645/1360590
相关文章推荐
- 使用反射+抽象工厂的数据访问(1)
- 使用反射+抽象工厂的数据访问(2)
- 使用反射+抽象工厂的数据访问(4)
- 转载:SQL through PowerShell 使用Powershell访问SQL并对数据进行读写增加删除操作
- 如何使用 ADO Stream 对象访问和修改 SQL Server BLOB 数据
- 使用Gzip压缩数据,加快页面访问速度
- 5.Android硬件访问服务使用反射
- 使用哪个 segment registers 进行数据的访问
- Linq中使用反射实现--LINQ通用数据表绑定DataGrid控件的方法(原创)
- 最近项目中flex数据访问使用的设计思想(续)
- 使用JavaScript访问XML数据
- 使用C语言访问MySQL数据 —— 执行SQL语句(2)
- Delphi使用ADO组件访问ACCESS数据入门例程
- vue.js中使用vueResource向后台请求数据时出现跨域访问失败的问题
- 7..使用反射动态创建数组和访问数组
- SQL SERVER 导入、导出数据到Exce(使用OpenRowset,、OpenDataSource函数)以及访问远程数据库(openrowset/opendatasource/openquery)
- 2.使用 Visual Studio Tools for Office(VSTO)访问Excel数据表
- (十)android 中数据存储与访问——使用SharedPreferences保存数据
- Spring Framework 开发参考手册 之十一 使用ORM工具进行数据访问
- android数据存储与访问之使用SD卡