Ejemplo n.º 1
0
        /// <summary>
        /// 从DataReader实例化一个Entity对
        /// </summary>
        /// <typeparam name="TA">实体类的类型TA</typeparam>
        /// <typeparam name="TB">实体类的类型TB</typeparam>
        /// <param name="reader">DataReader</param>
        /// <param name="entityPropertysA">已经排好序的实体类的属性集合TA</param>
        /// <param name="entityPropertysB">已经排好序的实体类的属性集合TB</param>
        /// <returns></returns>
        public static GenericPairEntity <TA, TB> FillOnePairEntity <TA, TB>(IDataReader reader)
            where TA : class, new()
            where TB : class, new()
        {
            List <PropertyInfo>        entityPropertyInfoA = EntityMappingTool.GetEntityPropertyInfos(typeof(TA));
            List <PropertyInfo>        entityPropertyInfoB = EntityMappingTool.GetEntityPropertyInfos(typeof(TB));
            GenericPairEntity <TA, TB> pair = new GenericPairEntity <TA, TB>();

            pair.EntityA = new TA();
            pair.EntityB = new TB();

            int offset = 0;

            for (int i = 0; i < entityPropertyInfoA.Count; i++)
            {
                if (reader.IsDBNull(offset + i))
                {
                    continue;
                }
                entityPropertyInfoA[i].SetValue(pair.EntityA, reader.GetValue(offset + i), null);
            }

            offset = entityPropertyInfoA.Count;
            for (int i = 0; i < entityPropertyInfoB.Count; i++)
            {
                if (reader.IsDBNull(offset + i))
                {
                    continue;
                }
                entityPropertyInfoB[i].SetValue(pair.EntityB, reader.GetValue(offset + i), null);
            }

            return(pair);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建用于删除的Sql命令
        /// </summary>
        public static DbCommand CreatDeleteCommand <T>(Database db, T entity)
        {
            Type          entityType = typeof(T);
            List <string> primaryKeyEntityFieldNames = EntityMappingTool.GetPrimaryKeyOfEntityField(entityType);
            List <string> primaryKeyDbCloumnNames    = EntityMappingTool.GetDbColumnNames(entityType,
                                                                                          primaryKeyEntityFieldNames);
            List <DbType> primaryKeyDbColumnTypes = EntityMappingTool.GetDbColumnTypes(entityType,
                                                                                       primaryKeyEntityFieldNames);
            List <PropertyInfo> primaryKeyPropertyInfos = EntityMappingTool.GetEntityPropertyInfos(entityType,
                                                                                                   primaryKeyEntityFieldNames);

            StringBuilder sqlBuilder = new StringBuilder();

            sqlBuilder.AppendFormat("DELETE FROM [{0}] WHERE ", EntityMappingTool.GetDbTableName(entityType));
            for (int i = 0; i < primaryKeyEntityFieldNames.Count; i++)
            {
                sqlBuilder.Append((i > 0) ? " AND " : "");
                sqlBuilder.AppendFormat("([{0}]=@{0})", primaryKeyDbCloumnNames[i]);
            }

            //参数
            DbCommand cmd = db.GetSqlStringCommand(sqlBuilder.ToString());

            for (int i = 0; i < primaryKeyEntityFieldNames.Count; i++)
            {
                db.AddInParameter(cmd, "@" + primaryKeyDbCloumnNames[i], primaryKeyDbColumnTypes[i],
                                  primaryKeyPropertyInfos[i].GetValue(entity, null));
            }

            return(cmd);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 检索出Entity实例中有设置值的属性
        /// </summary>
        /// <typeparam name="T">实体类的类型</typeparam>
        /// <param name="entity">实体类的实例</param>
        /// <param name="entityPropertyInfos">实体类对应的属性</param>
        /// <returns>Entity实例中有设置值的属性</returns>
        public static List <PropertyInfo> GetNotNullEntityPropertys <T>(T entity)
        {
            List <PropertyInfo> entityPropertyInfos    = EntityMappingTool.GetEntityPropertyInfos((typeof(T)));
            List <PropertyInfo> notNullEntityPropertys = new List <PropertyInfo>(entityPropertyInfos.Count);

            for (int i = 0; i < entityPropertyInfos.Count; i++)
            {
                if (entityPropertyInfos[i].GetValue(entity, null) != null)
                {
                    notNullEntityPropertys.Add(entityPropertyInfos[i]);
                }
            }

            return(notNullEntityPropertys);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 从DataReader实例化一个Entity
        /// </summary>
        /// <typeparam name="T">实体类的类型</typeparam>
        /// <param name="reader">DataReader</param>
        /// <param name="entityPropertyInfos">已经排好序的实体类的属性集合</param>
        /// <returns></returns>
        public static T FillOneEntity <T>(IDataReader reader) where T : class, new()
        {
            List <PropertyInfo> entityPropertyInfo = EntityMappingTool.GetEntityPropertyInfos(typeof(T));
            T entity = new T();

            for (int i = 0; i < entityPropertyInfo.Count; i++)
            {
                if (reader.IsDBNull(i))
                {
                    continue;
                }
                entityPropertyInfo[i].SetValue(entity, reader.GetValue(i), null);
            }

            return(entity);
        }