码迷,mamicode.com
首页 > 其他好文 > 详细

1111111

时间:2018-06-27 14:00:44      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:bsp   cmd   property   col   SQ   type   setvalue   stat   new   

public static T ReaderToEntity<T>(T objDest, SqlDataReader reader)
        {   
            PropertyInfo[] property = objDest.GetType().GetProperties();
            foreach (PropertyInfo p in property)
            {
                Type t = p.PropertyType;
                if(reader.GetValue(reader.GetOrdinal(p.Name)) != DBNull.Value)
                { 
                if (t == typeof(String) || t == typeof(int) || t == typeof(int?)
                    || t == typeof(long) || t == typeof(long?)
                    || t == typeof(DateTime) || t == typeof(DateTime?) || t == typeof(char) || t == typeof(decimal) || t == typeof(decimal?))
                    p.SetValue(objDest, reader.GetValue(reader.GetOrdinal(p.Name)));
                }
                else
                {
                    p.SetValue(objDest, null);
                }


            }
            return objDest;
}

=====调用
 SqlDataReader reader = cmd.ExecuteReader();
                        if (reader == null || reader.HasRows == false)
                            return null;
                        reader.Read();
                        MileageCalResult.AddParam addParam = new MileageCalResult.AddParam();
                        addParam = MileageCalResult.ReaderToEntity(addParam, reader);
                        MileageCalResult.AnalyseResult analyseResult = new MileageCalResult.AnalyseResult();
                        analyseResult =  MileageCalResult.ReaderToEntity(analyseResult, reader);
                        MileageCalResult obj = new MileageCalResult(addParam, analyseResult);

 

1111111

标签:bsp   cmd   property   col   SQ   type   setvalue   stat   new   

原文地址:https://www.cnblogs.com/rogge7/p/9233329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!