首页 新闻 会员 周边 捐助

DataReaderToObjlist 的报错问题

0
悬赏园豆:30 [待解决问题]


我这样操作的时候,
List<User_model> userlist=(List<User_model>)BLLHelper.DataReaderToObjlist<User_model>(MssqlHelper.ExecuteReader(ShopConfig_bll.GetShopConfig().DBConStrRead, sql));

总是在下面转换的过程中报错


class a{
public static List<T> DataTableToList<T>(DataTable dt) where T : class, new()
        {
            List<T> list = new List<T>();
            if (dt == null) return list;
            DataTableEntityBuilder<T> eblist = DataTableEntityBuilder<T>.CreateBuilder(dt.Rows[0]);
            foreach (DataRow info in dt.Rows) list.Add(eblist.Build(info));
            dt.Dispose(); dt = null;
            return list;
        }
        public static IList<T> DataReaderToObjlist<T>(SqlDataReader rdr)
        {
            IList<T> list = new List<T>();
            object tempValue = null;
            while (rdr.Read())
            {
                T t = System.Activator.CreateInstance<T>();
                Type obj = t.GetType();
                // 循环字段  
                for (int i = 0; i < rdr.FieldCount; i++)
                {
                    tempValue = null;
                    if (!rdr.IsDBNull(i))
                    {
                        tempValue = rdr.GetValue(i);
                    }
                    obj.GetProperty(rdr.GetName(i)).SetValue(t, tempValue, null);
                }
                list.Add(t);
            }
            rdr.Close();
            rdr.Dispose();
            return list;
        }
        public static object DataReaderToObj<T>(SqlDataReader rdr) where T : new()
        {
            T ObjT = System.Activator.CreateInstance<T>();
            Type obj = ObjT.GetType();
            object tempValue = null;
            if (rdr.Read())
            {
                // 循环字段  
                for (int i = 0; i < rdr.FieldCount; i++)
                {
                    tempValue = null;
                    if (!rdr.IsDBNull(i))
                    {
                        tempValue = rdr.GetValue(i);
                    }
                    else
                    {
                        string typeFullName = "";
                        try
                        {
                            typeFullName = obj.GetProperty(rdr.GetName(i)).PropertyType.FullName;
                            tempValue = GetDBNullValue(typeFullName);
                        }
                        catch
                        {

                        }
                        
                    }
                    obj.GetProperty(rdr.GetName(i)).SetValue(ObjT, tempValue, null);
                }
                rdr.Close();
                rdr.Dispose();
                return ObjT;
            }
            else
            {
                rdr.Close();
                rdr.Dispose();
                //return null;
                return new T();
            }


        }
        private static object GetDBNullValue(string typeFullName)
        {
            //typeFullName = typeFullName.ToLower();
            if (typeFullName == "System.String")//DbType.String.GetType().Name.ToLower()
            {
                return String.Empty;
            }
            else if (typeFullName == "System.Int32")
            {
                return 0;
            }
            else if (typeFullName == "System.DateTime")
            {
                return Convert.ToDateTime("1970-01-01");
            }
            else if (typeFullName == "System.Decimal")
            {
                return Convert.ToDecimal(0.00);
            }
            return null;
        }

}
public class DataTableEntityBuilder<Entity>
    {
        private static readonly MethodInfo getValueMethod = typeof(DataRow).GetMethod("get_Item", new Type[] { typeof(int) });
        private static readonly MethodInfo isDBNullMethod = typeof(DataRow).GetMethod("IsNull", new Type[] { typeof(int) });
        private delegate Entity Load(DataRow dataRecord);

        private Load handler;
        private DataTableEntityBuilder() { }

        public Entity Build(DataRow dataRecord)
        {
            return handler(dataRecord);
        }
        public static DataTableEntityBuilder<Entity> CreateBuilder(DataRow dataRecord)
        {
            DataTableEntityBuilder<Entity> dynamicBuilder = new DataTableEntityBuilder<Entity>();
            DynamicMethod method = new DynamicMethod("DynamicCreateEntity", typeof(Entity), new Type[] { typeof(DataRow) }, typeof(Entity), true);
            ILGenerator generator = method.GetILGenerator();
            LocalBuilder result = generator.DeclareLocal(typeof(Entity));
            generator.Emit(OpCodes.Newobj, typeof(Entity).GetConstructor(Type.EmptyTypes));
            generator.Emit(OpCodes.Stloc, result);

            for (int i = 0; i < dataRecord.ItemArray.Length; i++)
            {
                PropertyInfo propertyInfo = typeof(Entity).GetProperty(dataRecord.Table.Columns[i].ColumnName);
                Label endIfLabel = generator.DefineLabel();
                if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
                {
                    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, propertyInfo.PropertyType);
                    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;
        }
    }

枫-的主页 枫- | 初学一级 | 园豆:172
提问于:2014-12-13 23:12
< >
分享
所有回答(3)
0

  // 循环字段  
                for (int i = 0; i < rdr.FieldCount; i++)
                {
                    tempValue = null;
                    if (!rdr.IsDBNull(i))
                    {
                        tempValue = rdr.GetValue(i);
                    }
                    obj.GetProperty(rdr.GetName(i)).SetValue(t, tempValue, null);
                }

你这地方 对非null做处理了 null值呢?

wolfy | 园豆:2636 (老鸟四级) | 2014-12-14 14:02
0

浪费时间,Dapper直接上

吴瑞祥 | 园豆:29449 (高人七级) | 2014-12-14 21:10

之前一直以为Dapper是ORM,今天看了一下,

Dapper: a simple object mapper for .Net

才知道原来Dapper是集成了数据访问的AutoMapper,

怪不得用过L2S后,看着Dapper怎么都提不起兴趣来...

逃...

支持(0) 反对(0) 爱编程的大叔 | 园豆:30844 (高人七级) | 2014-12-15 15:28

@爱编程的大叔: L2S是什么?

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2014-12-15 17:30
0

我看到这么高大上的emit就不敢吭声了。

尤其是这代码太长……

爱编程的大叔 | 园豆:30844 (高人七级) | 2014-12-14 22:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册