public IDbCommand CreateCommand()
              {
                     ConstructorInfo info = m_typSqlCommand.GetConstructor(new Type[0]);
                     IDbCommand cmd = info.Invoke(null) as IDbCommand;
                     if ( cmd == null )
                            throw(new Exception("无法创建command "));
                     return cmd;
              }
 
              public DbDataAdapter CreateDataAdapter()
              {
                     ConstructorInfo info = m_typSqlDataAdapter.GetConstructor(new Type[0]);
                     DbDataAdapter da = info.Invoke(null) as DbDataAdapter;
                     if ( da == null )
                            throw(new Exception("无法创建adapter"));
                     return da;
              }
 
              public IDbDataParameter CreateParameter()
              {
                     ConstructorInfo info = m_typSqlParameter.GetConstructor(new Type[0]);
                     IDbDataParameter par = info.Invoke(null) as IDbDataParameter;
                     if ( par == null )
                            throw(new Exception("无法创建parameter constructor."));
                     return par;
              }
 
              public void DeriveParameters(IDbCommand cmd)
              {
                     object[] parameters = new object[1];
                     parameters[0] = cmd;
                     //m_typSqlBuilder.InvokeMember("DeriveParameters", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, parameters);
              }
       }
}
定义针对具体数据库的工厂类(SqlClientFactory.cs
SqlClientFactory类是一个具体的工厂类,由于继承了DbProviderFactory使代码量大大减少。这个工厂会生产出一个具体的产品,也就是这个类的实例。通过这个实例,就可以使用SqlConnectionSqlCommandSqlDataAdapterSqlParameterSqlCommandBuilder对象处理SQL Server数据库。
using System;
using System.Data;
using System.Data.Common;
namespace SplendidCRM
{
       public class SqlClientFactory : DbProviderFactory
       {
              // 在构造函数中调用父类的构造函数。必须指明操作该数据库的需要用到的重要的类。
public SqlClientFactory(string sConnectionString)
                     : base( sConnectionString
                           , "System.Data"
                           , "System.Data.SqlClient.SqlConnection"
                           , "System.Data.SqlClient.SqlCommand"   
                           , "System.Data.SqlClient.SqlDataAdapter"
                           , "System.Data.SqlClient.SqlParameter" 
                           , "System.Data.SqlClient.SqlCommandBuilder"
                           )
              {
              }
       }
}
 
 
如何使用
 
// 通过抽象工厂获取具体数据库工厂的实例,具体数据库通过字符串或Web.Config设置。
DbProviderFactory dbf = DbProviderFactories.GetFactory();
using ( IDbConnection con = dbf.CreateConnection() )
{
              con.Open();
              string sSQL;
              sSQL = "select NAME From Info  “;
              using ( IDbCommand cmd = con.CreateCommand() )
              {
                            cmd.CommandText = sSQL;
                            using ( IDataReader rdr = cmd.ExecuteReader() )
                            {
                                   while ( rdr.Read() )
                                                 {
                                                        ………
                                                 }
                            }
              }
}