| using System;
using System.Data;
using System.Data.Common;
using System.Data.SQLClient;
using System.Data.OleDb;
using System.Data.OracleClient;
namespace DAL
{
   public enum DatabaseType
   {
      Access,
      SQLServer,
      Oracle
      // 任何其他数据源类型
   }
   public enum ParameterType
   {
      Integer,
      Char,
      VarChar
      // 定义公用参数类型集
   }
   public class DataFactory
   {
      private DataFactory(){}
      public static IDbConnection CreateConnection
         (string ConnectionString, 
         DatabaseType dbtype)
      {
         IDbConnection cnn;
         switch(dbtype)
         {
            case DatabaseType.Access:
               cnn = new OleDbConnection
                  (ConnectionString); 
               break;
            case DatabaseType.SQLServer:
               cnn = new SQLConnection
                  (ConnectionString); 
               break;
            case DatabaseType.Oracle:
               cnn = new OracleConnection
                  (ConnectionString);
               break;
            default:
               cnn = new SQLConnection
                  (ConnectionString); 
               break;               
         }
         return cnn;
      }
      public static IDbCommand CreateCommand
         (string CommandText, DatabaseType dbtype,
         IDbConnection cnn)
      {
         IDbCommand cmd;
         switch(dbtype)
         {
            case DatabaseType.Access:
               cmd = new OleDbCommand
                  (CommandText,
                  (OleDbConnection)cnn);
               break;
            case DatabaseType.SQLServer:
               cmd = new SQLCommand
                  (CommandText,
                  (SQLConnection)cnn); 
               break;
            case DatabaseType.Oracle:
               cmd = new OracleCommand
                  (CommandText,
                  (OracleConnection)cnn);
               break;
            default:
               cmd = new SQLCommand
                  (CommandText,
                  (SQLConnection)cnn); 
               break;
         }
         return cmd;
      }
      public static DbDataAdapter CreateAdapter
         (IDbCommand cmd, DatabaseType dbtype)
      {
         DbDataAdapter da;
         switch(dbtype)
         {
            case DatabaseType.Access:
               da = new OleDbDataAdapter
                  ((OleDbCommand)cmd); 
               break;
            case DatabaseType.SQLServer:
               da = new SQLDataAdapter
                  ((SQLCommand)cmd); 
               break;
            case DatabaseType.Oracle:
               da = new OracleDataAdapter
                  ((OracleCommand)cmd); 
               break;
            default:
               da = new SQLDataAdapter
                  ((SQLCommand)cmd); 
               break;
         }
         return da;
      }
   }
} |