/// <summary>
        /// 查询集合,带分页
        /// </summary>
        /// <param name="item">框架实体</param>
        /// <param name="dp">分页对象</param>
        /// <returns>对象集合</returns>
        public List <T> SelectList(FrameWorkItem item, DataPage dp = null)
        {
            IDataReader dr = null;

            try
            {
                dr = BuildDataReader(item, dp);

                List <T> list = new List <T>();
                if (dp != null && dp.PageSize > 0)
                {
                    int result = GetResult <int>(string.Format("SELECT COUNT(1) FROM ({0}) a", item.Sql), item.ConnectionString, item.SqlParam);
                    dp.RowCount = result;
                }
                while (dr != null && dr.Read())
                {
                    T tempT = new T();
                    tempT = DynamicBuilderEntity <T> .CreateBuilder(dr).Build(dr);

                    list.Add(tempT);
                }
                return(list);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
        }
        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="item">框架实体</param>
        /// <returns>单个对象</returns>
        public T SelectModel(FrameWorkItem item)
        {
            IDataReader dr = null;

            try
            {
                dr = BuildDataReader(item);
                if (dr != null && dr.Read())
                {
                    return(DynamicBuilderEntity <T> .CreateBuilder(dr).Build(dr));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return(default(T));
        }
Esempio n. 3
0
        /// <summary>
        /// 建立过程
        /// </summary>
        /// <param name="dataRecord">取数据流字段</param>
        /// <returns></returns>
        public static DynamicBuilderEntity <T> CreateBuilder(IDataRecord dataRecord)
        {
            var dynamicBuilder = new DynamicBuilderEntity <T>();

            var         method    = new DynamicMethod("DynamicCreateEntity", typeof(T), new[] { typeof(IDataRecord) }, typeof(T), true);
            ILGenerator generator = method.GetILGenerator();

            LocalBuilder result = generator.DeclareLocal(typeof(T));

            generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
            generator.Emit(OpCodes.Stloc, result);

            for (int i = 0; i < dataRecord.FieldCount; i++)
            {
                var propertyInfo = typeof(T).GetProperty(dataRecord.GetName(i));
                if (propertyInfo == null)
                {
                    continue;
                }
                var endIfLabel = generator.DefineLabel();

                if (propertyInfo.GetSetMethod() == null)
                {
                    continue;
                }
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldc_I4, i);
                generator.Emit(OpCodes.Callvirt, isDBNullMethod);
                generator.Emit(OpCodes.Brtrue, endIfLabel);
                generator.Emit(OpCodes.Ldloc, result);
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldc_I4, i);
                generator.Emit(OpCodes.Callvirt, getValueMethod);
                generator.Emit(OpCodes.Unbox_Any, dataRecord.GetFieldType(i));
                generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());
                generator.MarkLabel(endIfLabel);
            }
            generator.Emit(OpCodes.Ldloc, result);
            generator.Emit(OpCodes.Ret);

            dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load));
            return(dynamicBuilder);
        }