Esempio n. 1
0
        /// <summary>
        /// 查找满足条件的实体集合
        /// </summary>
        /// <typeparam name="T">实体对象参数类型</typeparam>
        /// <param name="filterentity">查询条件对象(也可以用null代替)</param>
        /// <param name="filterclause">查询条件(多个条件的情况下,可以用“,”隔开,也可以用null代替)</param>
        /// <param name="storageprocedure">存储过程名称</param>
        /// <returns></returns>
        public static List <T> FillEntities <T>(object filterentity, string filterclause, string storageprocedure)
            where T : class, new()
        {
            T        entityType = new T();
            Type     type       = entityType.GetType();
            List <T> coll       = new List <T>();

            XMLMapping xmlMapping = new XMLMapping();

            //获取类型的映射信息
            MappingInfo mapInfo = xmlMapping.GetDataMapInfo(type);

            Database db = new Database();

            db.CommandText = storageprocedure;

            //获取查询条件的映射信息
            if (filterentity != null)   //如果查询条件不为null,则进行查询条件的映射,并且为查询语句增加查询参数
            {
                Type        filterEntityType = filterentity.GetType();
                MappingInfo filterMapInfo    = xmlMapping.GetSearchConditionDataMapInfo(filterEntityType);
                filterMapInfo.CurrentOperation = db.CommandText;
                addParameters(db, filterMapInfo, filterclause, filterentity);
            }

            IDataReader reader = db.GetDataReader();

            List <T> entities  = new List <T>();
            T        entityout = null;

            while (reader.Read())
            {
                //T obj = new T();
                entityout = xmlMapping.CreateObject <T>(reader, mapInfo);
                //entities.Add(entityout);
                entities.Add(entityout);
            }
            db.Close();
            return(entities);
        }