连接不同的数据库

using System;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Data.SqlClient;

namespace ASPNETClassLibrary
{
 /// <summary>
 /// GetCommand 的摘要说明。
 /// </summary>
 public class GetCommand
 {

  /// <summary>
  /// 构造函数
  /// </summary>
  public GetCommand()
  {
   
  }


  /// <summary>
  /// 连接创建数据库连接对象
  /// </summary>
  /// <param name="connectionString">连接数据库字符串</param>
  /// <param name="dataBaseType">数据库类型</param>
  /// <returns>Connection</returns>
  public static IDbConnection CreateConnection(string connectionString,string dataBaseType)
  {
   IDbConnection cn ;
   switch(dataBaseType)
   {
    case "Odbc" :
     cn = new OdbcConnection();
     break;
    case "OleDb":
     cn = new OleDbConnection();
     break;
    case "Oracle":
     cn = new OracleConnection();
     break;
    case "Sql":
     cn = new SqlConnection();
     break;
    default:
     cn = new SqlConnection();
     break;
   }//End switch
   cn.ConnectionString = connectionString;
   return cn;
  }

 
  /// <summary>
  /// 创建命令集对象
  /// </summary>
  /// <param name="commandText">要执行的查询语句、存储过程名称</param>
  /// <param name="dataBaseType">数据库类型</param>
  /// <param name="cn">连接对象</param>
  /// <returns>Command</returns>
  public static IDbCommand CreateCommand(string executeString,string dataBaseType,IDbConnection cn)
  {
   IDbCommand cmd;
   switch(dataBaseType)
   {
    case "Odbc" :
     cmd = new OdbcCommand(executeString, (OdbcConnection)cn);
     break;
    case "OleDb":
     cmd = new OleDbCommand(executeString, (OleDbConnection)cn);
     break;
    case "Oracle":
     cmd = new OracleCommand(executeString, (OracleConnection)cn);
     break;
    case "Sql":
     cmd = new SqlCommand(executeString, (SqlConnection)cn);
     break;
    default:
     cmd = new SqlCommand(executeString, (SqlConnection)cn);
     break;
   }//End switch;
   return cmd;
  }

 
  /// <summary>
  /// 返回一个数据集对象
  /// </summary>
  /// <param name="cmd">命令对象</param>
  /// <param name="dataBaseType">数据库类型</param>
  /// <returns>IDataAdapter</returns>
  public static IDataAdapter CreateIDataAdapter(IDbCommand cmd, string dataBaseType)
  {
   IDataAdapter ida ;
   switch(dataBaseType)
   {
    case "Odbc" :
     ida = new OdbcDataAdapter((OdbcCommand)cmd);
     break;
    case "OleDb":
     ida = new OleDbDataAdapter((OleDbCommand)cmd);
     break;
    case "Oracle":
     ida = new OracleDataAdapter((OracleCommand)cmd);
     break;
    case "Sql":
     ida = new SqlDataAdapter((SqlCommand)cmd);
     break;
    default:
     ida = new SqlDataAdapter((SqlCommand)cmd);
     break;
   }//End switch;
   return ida;
  }

  /// <summary>
  /// 返回一个数据集对象
  /// </summary>
  /// <param name="cmd">命令对象</param>
  /// <param name="dataBaseType">数据库类型</param>
  /// <returns>DbDataAdapter </returns>
  public static DbDataAdapter CreateDbDataAdapter(IDbCommand cmd,string dataBaseType)
  {
   DbDataAdapter dda;
   switch(dataBaseType)
   {
    case "Odbc" :
     dda = new OdbcDataAdapter((OdbcCommand)cmd);
     break;
    case "OleDb":
     dda = new OleDbDataAdapter((OleDbCommand)cmd);
     break;
    case "Oracle":
     dda = new OracleDataAdapter((OracleCommand)cmd);
     break;
    case "Sql":
     dda = new SqlDataAdapter((SqlCommand)cmd);
     break;
    default:
     dda = new SqlDataAdapter((SqlCommand)cmd);
     break;
   }//End switch;
   return dda;
  }
 }
}
 

posted on   小土泥  阅读(206)  评论(0编辑  收藏  举报

导航

< 2006年8月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9
点击右上角即可分享
微信分享提示