• DataReader转实体<T>



            #region DataReader转实体<T>

            /// <summary>
            /// DataReader转实体
            /// </summary>
            /// <typeparam name="T">实体类型</typeparam>
            /// <param name="dr">DataReader</param>
            /// <returns>实体对象</returns>
            public static T DataReaderToEntity<T>(IDataReader dr) where T : new()
            {
                T t = new T();
                if (dr == null) return default(T);
                using (dr)
                {
                    if (dr.Read())
                    {
                        // 获得实体的公共属性
                        PropertyInfo[] propertys = t.GetType().GetProperties();
                        List<string> listFieldName = new List<string>(dr.FieldCount);
                        for (int i = 0; i < dr.FieldCount; i++)
                        {
                            listFieldName.Add(dr.GetName(i).ToLower());
                        }

                        foreach (PropertyInfo p in propertys)
                        {
                            string columnName = p.Name;
                            if (listFieldName.Contains(columnName.ToLower()))
                            {
                                // 判断此属性是否有Setter或columnName值是否为空
                                object value = dr[columnName];
                                if (!p.CanWrite || value is DBNull || value == DBNull.Value) continue;
                                try
                                {
                                    #region SetValue
                                    switch (p.PropertyType.ToString())
                                    {
                                        case "System.String":
                                            p.SetValue(t, Convert.ToString(value), null);
                                            break;
                                        case "System.Int32":
                                            p.SetValue(t, Convert.ToInt32(value), null);
                                            break;
                                        case "System.Int64":
                                            p.SetValue(t, Convert.ToInt64(value), null);
                                            break;
                                        case "System.DateTime":
                                            p.SetValue(t, Convert.ToDateTime(value), null);
                                            break;
                                        case "System.Boolean":
                                            p.SetValue(t, Convert.ToBoolean(value), null);
                                            break;
                                        case "System.Double":
                                            p.SetValue(t, Convert.ToDouble(value), null);
                                            break;
                                        case "System.Decimal":
                                            p.SetValue(t, Convert.ToDecimal(value), null);
                                            break;
                                        default:
                                            p.SetValue(t, value, null);
                                            break;
                                    }
                                    #endregion
                                }
                                catch
                                {
                                    //throw (new Exception(ex.Message));
                                }
                            }
                        }
                    }
                }
                return t;
            }
            #endregion

  • 相关阅读:
    时装画基础知识--如何画人体
    马士兵java视频学习顺序
    Mysql 中文字符乱码问题
    zendstudio 设置默认编码 utf-8 gbk
    MYSQL 本地无ROOT权限 忘记密码
    windows 3389 远程
    windows 老掉牙CMD的命令
    mysql-常用注入渗透手法
    ubuntu 添加多个IP
    windows下简单配置apache
  • 原文地址:https://www.cnblogs.com/single/p/2745806.html
Copyright © 2020-2023  润新知