• 连接不同的数据库


    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;
      }
     }
    }
     

  • 相关阅读:
    (转)ASP.NET-关于Container dataitem 与 eval方法介绍
    WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)
    转载 C# BindingSource
    repeater控件实现分页
    JS操作SELECT方法
    C#中的Invoke
    ios深拷贝,浅拷贝,拷贝自定义对象的简单介绍(转)
    IOS的一些尺寸
    ASP.NET多线程下使用HttpContext.Current
    一些xcode5.1创建的工程在xcode6.0下不能编译的问题
  • 原文地址:https://www.cnblogs.com/xiaotuni/p/2365828.html
Copyright © 2020-2023  润新知