Ejemplo n.º 1
0
        /// <summary>
        /// 获取实体对象。
        /// </summary>
        /// <typeparam name="TEntity">实体对象类型</typeparam>
        /// <param name="dr">DbDataReader</param>
        /// <param name="entity">实体对象</param>
        /// <param name="metaDataTable">MetaDataTable对象</param>
        /// <returns>实体对象</returns>
        public static TEntity GetEntity <TEntity>(DbDataReader dr, TEntity entity, MetaDataTable metaDataTable)
        {
            for (int i = 0; i < dr.FieldCount; i++)
            {
                string colName = dr.GetName(i).ToLower();
                if (!metaDataTable.Columns.ContainsKey(colName))
                {
                    continue;
                }

                MetaDataColumn metaColumn = metaDataTable.Columns[colName];
                if (!dr.IsDBNull(i))
                {
                    object value = Convert.ChangeType(dr.GetValue(i),
                                                      IsNullableType(metaColumn.DataType) ?
                                                      metaColumn.DataType.GetGenericArguments()[0] : metaColumn.DataType);
                    metaColumn.Member.SetValue(entity, value, null);
                }
            }

            return(entity);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取实体属性与值的对应表集合。
        /// </summary>
        /// <typeparam name="TEntity">实体对象类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <param name="entityName">实体名称</param>
        /// <param name="columnNames">属性列名称</param>
        /// <returns>实体属性与值的对应表集合</returns>
        public static DataFieldMapTable GetMapTable <TEntity>(TEntity entity, string entityName, params string[] columnNames)
        {
            var metaDataTable = new MetaDataTable(typeof(TEntity), entityName);

            if (columnNames != null && columnNames.Length > 0)
            {
                return(GetMapTableByColumnNames <TEntity>(entity, metaDataTable, columnNames));
            }

            DataFieldMapTable mapTable = new DataFieldMapTable(metaDataTable.Columns.Count);

            foreach (string key in metaDataTable.Columns.Keys)
            {
                var metaColumn = metaDataTable.Columns[key];
                if (IsIgnored(metaColumn))
                {
                    continue;
                }

                mapTable.Add(metaColumn.Name, GetColumnValue(metaColumn, entity));
            }

            return(mapTable);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取实体对象。
        /// </summary>
        /// <typeparam name="TEntity">实体对象类型</typeparam>
        /// <param name="dr">DbDataReader</param>
        /// <param name="entity">实体对象</param>
        /// <param name="entityName">实体名称</param>
        /// <returns>实体对象</returns>
        public static TEntity GetEntity <TEntity>(DbDataReader dr, TEntity entity, string entityName)
        {
            var metaTable = new MetaDataTable(typeof(TEntity), entityName);

            return(GetEntity <TEntity>(dr, entity, metaTable));
        }
Ejemplo n.º 4
0
        private static DataFieldMapTable GetMapTableByColumnNames <TEntity>(TEntity entity, MetaDataTable metaDataTable,
                                                                            params string[] columnNames)
        {
            DataFieldMapTable mapTable = new DataFieldMapTable(columnNames.Length);

            foreach (string columnName in columnNames)
            {
                string colName = columnName.Trim().ToLower();
                if (!metaDataTable.Columns.ContainsKey(colName) ||
                    IsIgnored(metaDataTable.Columns[colName]))
                {
                    continue;
                }

                var metaColumn = metaDataTable.Columns[colName];
                mapTable.Add(metaColumn.Name, GetColumnValue(metaColumn, entity));
            }

            return(mapTable);
        }