珂珂的个人博客 - 一个程序猿的个人网站

一个通用的数据库访问组件(三)

一个通用的数据库访问组件(一)

一个通用的数据库访问组件(二)

一个通用的数据库访问组件(三)

      最后一个类DbHelper 继承DbHelperAbstract

using System;
using System.Data.Common;
using System.Data;
using System.Collections.Generic;
using FYJ.Data.Util;

namespace FYJ.Data
{
    public class DbHelper : DbHelperAbstract
    {
        private DbTransaction tran;

        #region 构造函数
        public DbHelper()
        {

        }
        public DbHelper(string connectionName)
            : base(connectionName)
        {

        }
        public DbHelper(DbProviderFactory factory, string connectionString)
            : base(factory, connectionString)
        {

        }

        public DbHelper(string providerName, string connectionString)
            : base(providerName, connectionString)
        {

        }
        #endregion

       

        public override DbTransaction Tran
        {
            get { return tran; }
        }

        public override void BeginTran()
        {
            DbConnection conn = this.DbProviderFactoryInstance.CreateConnection();
            conn.ConnectionString = this.ConnectionString;
            conn.Open();
            this.tran = conn.BeginTransaction();
        }

        public override void Commit()
        {
            if (tran != null)
            {
                if (this.tran.Connection != null)
                {
                    this.tran.Commit();
                }

                if (tran.Connection != null)
                {
                    tran.Connection.Close();
                }

                tran = null;
            }
        }

        public override void Rollback()
        {
            if (tran != null)
            {
                if (this.tran.Connection != null)
                {
                    this.tran.Rollback();
                }

                if (tran.Connection != null)
                {
                    tran.Connection.Close();
                }

                tran = null;
            }
        }

        public override DataSet RunProcedure(string storedProcName, Dictionary<string, object> dic)
        {
            throw new NotImplementedException();
        }

        public override DataSet RunProcedure(IEnumerable<IDataParameter> parameters, string storedProcName)
        {
            DbConnection conn = null;
            if (tran != null)
            {
                conn = tran.Connection;
            }
            else
            {
                conn = this.DbProviderFactoryInstance.CreateConnection();
                conn.ConnectionString = this.ConnectionString;
                conn.Open();
            }

            conn.ConnectionString = this.ConnectionString;
            conn.Open();
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = storedProcName;
            DataSet ds = new DataSet();
            DbDataAdapter adapter = this.DbProviderFactoryInstance.CreateDataAdapter();
            if (parameters != null)
            {
                foreach (DbParameter parameter in Helper.FixParametersPre(parameters, this.DbHelperTypeEnum))
                {
                    cmd.Parameters.Add(parameter);
                }
            }
            adapter.SelectCommand = cmd;
            adapter.Fill(ds);
            if (tran == null)
            {
                conn.Close();
            }
            return ds;
        }

        public override int ExecuteProcedure(string storedProcName, Dictionary<string, object> dic)
        {
            if (this.DbHelperTypeEnum == DbHelperType.SqlServer)
            {
                RunProcedure(storedProcName, dic);
                return Convert.ToInt32(dic["@RETURN_VALUE"]);
            }
            throw new NotImplementedException();
        }

        public override int ExecuteProcedure(IEnumerable<IDataParameter> parameters, string storedProcName)
        {
            throw new NotImplementedException();
        }

        public override DataSet GetDataSet(IEnumerable<IDataParameter> parms, string sql)
        {
            DbConnection conn = this.DbProviderFactoryInstance.CreateConnection();
            conn.ConnectionString = this.ConnectionString;
            conn.Open();
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            DbDataAdapter adapter = this.DbProviderFactoryInstance.CreateDataAdapter();
            if (parms != null)
            {
                foreach (IDataParameter parm in Helper.FixParametersPre(parms, this.DbHelperTypeEnum))
                {
                    cmd.Parameters.Add(parm);
                }
            }
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Parameters.Clear();
            conn.Close();

            return ds;
        }

        public override int ExecuteSql(IEnumerable<System.Data.IDataParameter> parms, string sql)
        {
            DbConnection conn = null;
            if (tran != null)
            {
                conn = tran.Connection;
            }
            else
            {
                conn = this.DbProviderFactoryInstance.CreateConnection();
                conn.ConnectionString = this.ConnectionString;
                conn.Open();
            }

            DbCommand cmd = conn.CreateCommand();
            cmd.Transaction = tran;
            if (parms != null)
            {
                foreach (DbParameter parameter in parms)
                {
                    if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
                        (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(parameter);
                }
            }
            cmd.CommandText = sql;
            int rows = cmd.ExecuteNonQuery();

            if (tran == null)
            {
                conn.Close();
            }
            return rows;
        }
    }
}



上一篇:URL 签名验证

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧