日期:2014-05-17  浏览次数:20540 次

ADO.NET通用DBHelper类,自己这两天琢磨的,和大家分享下,主要是希望有高手能指点下,好让我改进!
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Reflection;

namespace WebDAL
{

    public class DBHelper
    {
        #region 属性

        //项目中添加如下引用
        //System.Configuration
        //System.Data.OracleClient


        //web.config中增加如下节点
        //<appSettings>
        //<add key="DBType" value="SQLServer"/>配置数据库类型SQLServer||OleDb||ODBC||Oracle
        //<add key="SQLServer" value="连库字符串"/>配置该数据库类型对应的连库字符串
        //<add key="assemblyName" value="Entity"/>指定实体类的命名空间
        //</appSettings>

        //获取连接字符串和数据库类型
        //从Web.config文件中动态获取
        
        private static string conType=ConfigurationManager.AppSettings["DBType"].ToString();

        private static string constr = ConfigurationManager.AppSettings[conType].ToString();

        //动态获取程序集名称,以备反射时所用
        private static string assemblyName = ConfigurationManager.AppSettings["assemblyName"].ToString();
        #endregion

        #region 私有方法

        /// <summary>
        /// 根据数据库类型,获取对应数据库的连接
        /// </summary>
        /// <returns>连接接口</returns>
        private static IDbConnection GetConnection()
        {
            IDbConnection con = null;
            if (conType == DBType.SQLServer.ToString())
            {
                con = new SqlConnection(constr);
            }
            else if (conType == DBType.Oracle.ToString())
            {
                con = new OracleConnection(constr);
            }
            else if (conType == DBType.OleDb.ToString())
            {
                con = new OleDbConnection(constr);
            }
            else if (conType == DBType.ODBC.ToString())
            {
                con = new OdbcConnection(constr);
            }
            else
            {
                con = new SqlConnection(constr);
            }
            return con;
        }

        /// <summary>
        /// 根据数据库类型,获取对应Command对象
        /// </summary>
        /// <param name="cmdText">SQL语句或存储过程名</param>
        /// <param name="cmdType">SQL命令类型</param>
        /// <param name="con">连接对象</param>
        /// <param name="param">SQL命令参数数组</param>
        /// <returns>Command接口对象</returns>
        private static IDbCommand GetCommand(string commandText,CommandType commandType,IDbConnection con, params IDbDataParameter[] param)
        {
            IDbCommand cmd = null;
            if (conType == DBType.SQLServer.ToString())
            {
                cmd = new SqlCommand(commandText,con as SqlConnection);
            }
            else if (conType == DBType.Oracle.ToString())
            {
                cmd = new OracleCommand(commandText, con as OracleConnection);
            }
            else if (conType == DBType.OleDb.ToString())
            {
                cmd = new OleDbCommand(commandText,con as OleDbConnection);
            }
            else if (conType == DBType.ODBC.ToString())
            {
                cmd = new OdbcCommand(commandText,con as OdbcConnection);
            }
            else
            {
                cmd = new SqlCommand(commandText, con as SqlConnection);
            }
            cmd.CommandType = commandType;
            if (param != null)
            {
                cmd.Parameters.Add(param);
            }
            return cmd;
        }

        /// <summary>
        /// 执行返回一条记录的泛型集合对象
        /// </summary>