WEB开发网
开发学院数据库MSSQL Server 教你如何来进行编写通用的数据访问 阅读

教你如何来进行编写通用的数据访问

 2007-05-19 09:42:08 来源:WEB开发网   
核心提示: using System;using System.Data;using System.Data.Common;using System.Data.SqlClient;using System.Data.OleDb;using System.Data.OracleClient;namesp
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;
   }
  }
}

上一页  1 2 3 4 5 6 7 8 9  下一页

Tags:如何 进行 编写

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接