连接不同的数据库
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;
}
}
}
【推荐】中国电信天翼云云端翼购节,2核2G云服务器一口价38元/年
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步