Ejemplo n.º 1
0
        /// <summary>
        /// 获得指定Sql语句查询下的数据集
        /// </summary>
        /// <param name="dbCommand">数据库命令</param>
        /// <param name="connection">数据库连接</param>
        /// <param name="transaction">数据库事务</param>
        /// <exception cref="ArgumentNullException">数据库命令不能为空</exception>
        /// <returns>数据集</returns>
        public DataSet ExecuteDataSet(DbCommand dbCommand, DbConnection connection, DbTransaction transaction)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            DataSet dataSet = null;

            if (connection == null && transaction == null)
            {
                using (DatabaseConnectionWrapper wrapper = this.InternalGetConnection())
                {
                    dbCommand.Connection = wrapper.Connection;

                    dataSet = DataSetHelper.InternalCreateDataSet(this._dbProvider, dbCommand);
                }
            }
            else
            {
                this.AddTransactionAndConnectionToDbCommand(dbCommand, connection, transaction);

                dataSet = DataSetHelper.InternalCreateDataSet(this._dbProvider, dbCommand);
            }

            return(dataSet);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 返回执行指定Sql语句后影响的行数
        /// </summary>
        /// <param name="dbCommand">数据库命令</param>
        /// <param name="connection">数据库连接</param>
        /// <param name="transaction">数据库事务</param>
        /// <exception cref="ArgumentNullException">数据库命令不能为空</exception>
        /// <returns>受影响的行数</returns>
        public Int32 ExecuteNonQuery(DbCommand dbCommand, DbConnection connection, DbTransaction transaction)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            Int32 result;

            if (connection == null && transaction == null)
            {
                using (DatabaseConnectionWrapper wrapper = this.InternalGetConnection())
                {
                    dbCommand.Connection = wrapper.Connection;

                    result = dbCommand.ExecuteNonQuery();
                }
            }
            else
            {
                this.AddTransactionAndConnectionToDbCommand(dbCommand, connection, transaction);

                result = dbCommand.ExecuteNonQuery();
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 使用数据库读取器执行操作
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="command">指定Sql语句</param>
        /// <param name="function">使用数据库读取器的操作</param>
        /// <returns>返回的内容</returns>
        public T UsingDataReader <T>(ISqlCommand command, Func <IDataReader, T> function)
        {
            DatabaseConnectionWrapper wrapper = null;
            IDataReader reader = null;

            try
            {
                wrapper = this.InternalGetConnection();

                reader = this.ExecuteReader(command, wrapper.Connection);
                return(function(reader));
            }
            catch
            {
                throw;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }

                if (wrapper != null)
                {
                    wrapper.Dispose();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获得指定Sql语句查询下的数据集
        /// </summary>
        /// <param name="dbCommand">数据库命令</param>
        /// <exception cref="ArgumentNullException">数据库命令不能为空</exception>
        /// <returns>数据集</returns>
        public DataSet ExecuteDataSet(DbCommand dbCommand)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            DataSet dataSet = null;

            using (DatabaseConnectionWrapper wrapper = this.GetConnection())
            {
                dbCommand.Connection = wrapper.Connection;
                dataSet = DataSetHelper.InternalCreateDataSet(this._dbProvider, dbCommand);
            }

            return(dataSet);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 获得指定Sql语句查询下的数据读取器
        /// </summary>
        /// <param name="dbCommand">数据库命令</param>
        /// <exception cref="ArgumentNullException">数据库命令不能为空</exception>
        /// <returns>数据读取器</returns>
        public IDataReader ExecuteReader(DbCommand dbCommand)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            IDataReader result;

            using (DatabaseConnectionWrapper wrapper = this.GetConnection())
            {
                dbCommand.Connection = wrapper.Connection;
                result = dbCommand.ExecuteReader();
            }

            return(result);
        }
Ejemplo n.º 6
0
        internal DatabaseConnectionWrapper InternalGetConnection()
        {
            DatabaseConnectionWrapper connection = TransactionScopeConnections.GetConnection(this);

            return(connection ?? this.InternalGetNewConnection());
        }