Beispiel #1
0
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <typeparam name="T">返回对象类型</typeparam>
        /// <param name="command">查询命令</param>
        /// <param name="closeConnection">关闭数据库连接</param>
        /// <returns></returns>
        protected virtual T QueryForObject <T>(IDbCommand command, bool closeConnection)
        {
            var         @delegate = DataRecordBuilder <T> .Func;
            IDataReader reader    = null;

            try
            {
                using (reader = this.CreateReader(command))
                {
                    var rd = new IDataRecordDecorator(reader);
                    if (reader.Read())
                    {
                        return(@delegate(rd.Load(reader)));
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (this.Transaction == null && closeConnection && reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
            }

            return(default(T));
        }
Beispiel #2
0
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <typeparam name="T">返回对象类型</typeparam>
        /// <param name="command">查询命令</param>
        /// <param name="closeConnection">关闭数据库连接</param>
        /// <returns></returns>
        protected virtual IEnumerable <T> QueryForEnumerable <T>(IDbCommand command, bool closeConnection)
        {
            var         @delegate = DataRecordBuilder <T> .Func;
            var         result    = new List <T>();
            IDataReader reader    = null;

            try
            {
                using (reader = this.CreateReader(command))
                {
                    var rd = new IDataRecordDecorator(reader);
                    while (reader.Read())
                    {
                        var value = @delegate(rd.Load(reader));
                        result.Add(value);
                    }
                }
                return(result);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (this.Transaction == null && closeConnection && reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
            }
        }