Ejemplo n.º 1
0
        /// <summary>
        /// 返回满足查询条件的实体,如果没有满足要求的实体,则返回null
        /// </summary>
        /// <typeparam name="T">目标实体类型</typeparam>
        /// <param name="filterentity">查询对象(也可以用null代替)</param>
        /// <param name="filterclause">查询条件(多个条件的情况下,可以用“,”隔开,也可以用null代替)</param>
        /// <param name="storageprocedure">存储过程名称</param>
        /// <returns></returns>
        public static T FillEntity <T>(object filterentity, string filterclause, string storageprocedure)
            where T : class, new()
        {
            T          entity     = new T();
            XMLMapping xmlMapping = new XMLMapping();
            Type       type       = entity.GetType();

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

            Database db = new Database();

            db.CommandText = storageprocedure;

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

            IDataReader reader = db.GetDataReader();

            T entityout = null;

            if (reader.Read())
            {
                entityout = xmlMapping.CreateObject <T>(reader, mapInfo);
            }

            return(entityout);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 根据条件更新实体集合
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities">待更新的实体集合</param>
        /// <param name="filterclause">更新参数(多个条件的情况下,可以用“,”隔开,也可以用null代替)</param>
        /// <param name="storageprocedure">存储过程</param>
        /// <returns>返回受影响的记录数量</returns>
        public static int UpdateEntities <T>(List <T> entities, string filterclause, string storageprocedure)
            where T : class, new()
        {
            int result = -1;

            if (entities != null && entities.Count > 0)
            {
                XMLMapping xmlMapping = new XMLMapping();
                T          entityType = new T();

                //获取类型的映射信息
                MappingInfo mapInfo = xmlMapping.GetDataMapInfo(entityType.GetType());
                Database    db      = new Database();
                mapInfo.CurrentOperation = storageprocedure;
                addParameters(db, mapInfo, filterclause);
                IDbCommand command = db.GetCommand();
                command.CommandText = storageprocedure;
                for (int i = 0; i < entities.Count; i++)
                {
                    addParametersValue(db, entities[i], mapInfo);
                    command.ExecuteNonQuery();
                    result++;
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="filterClause"></param>
        /// <param name="storageprocedure">存储过程</param>
        /// <returns>返回操作所影响的行数,如果为-1,则表示更新失败</returns>

        /// <summary>
        /// 具有返回的更新实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">更新的对象</param>
        /// <param name="filterClause">更新的对象的属性(多个条件的情况下,可以用“,”隔开,也可以用null代替)</param>
        /// <param name="returnClause">返回的对象属性(多个条件的情况下,可以用“,”隔开,也可以用null代替)</param>
        /// <param name="storageprocedure">使用的存储过程</param>
        /// <returns>返回的属性与return中的对象属性对应</returns>
        public static T UpdateEntityWithReturn <T>(object entity, string filterClause, string returnClause, string storageprocedure)
            where T : class, new()
        {
            XMLMapping xmlMapping = new XMLMapping();

            //获取类型的映射信息
            MappingInfo mapInfo = xmlMapping.GetDataMapInfo(entity.GetType());
            Database    db      = new Database();

            db.CommandText           = storageprocedure;
            mapInfo.CurrentOperation = db.CommandText;
            addParameters(db, mapInfo, filterClause, returnClause, entity);
            IDbCommand    command = db.GetCommand();
            int           result;
            StringBuilder errorMessages = new StringBuilder();

            T entityout = null;

            try
            {
                result = command.ExecuteNonQuery();
                //Type t = entity.GetType().
                entityout = xmlMapping.CreateObject <T>((SqlCommand)command, mapInfo);
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    errorMessages.Append("Index #" + i + "\n" +
                                         "Message: " + ex.Errors[i].Message + "\n" +
                                         "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                                         "Source: " + ex.Errors[i].Source + "\n" +
                                         "Procedure: " + ex.Errors[i].Procedure + "\n");
                }


                throw new Exception(errorMessages.ToString());
                Console.WriteLine(errorMessages.ToString());
                Console.ReadLine();
            }
            return(entityout);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取一个默认实体集,可用于测试实体错误及显示表头
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List <T> FillDefaulValueRow <T>()
            where T : class, new()
        {
            T                   entity     = new T();
            XMLMapping          xmlMapping = new XMLMapping();
            Type                type       = entity.GetType();
            MappingInfo         mapInfo    = xmlMapping.GetDataMapInfo(type);
            PropertyMappingInfo pmi        = null;
            List <T>            entities   = new List <T>();

            for (int i = 0; i < mapInfo.Properties.Count; i++)
            {
                pmi = (PropertyMappingInfo)mapInfo.Properties[i];
                object propertyObj = Convert.ChangeType(pmi.PropertyDefaultValue, entity.GetType().GetProperty(pmi.PropertyName).PropertyType);
                entity.GetType().GetProperty(pmi.PropertyName).SetValue(entity, propertyObj, null);
            }

            entities.Add(entity);
            return(entities);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">更新的对象</param>
        /// <param name="filterClause">更新的对象的属性(多个条件的情况下,可以用“,”隔开,也可以用null代替)</param>
        /// <param name="storageprocedure">存储过程</param>
        /// <returns>返回操作所影响的行数,如果为-1,则表示更新失败</returns>
        public static int UpdateEntity(object entity, string filterClause, string storageprocedure)
        {
            XMLMapping xmlMapping = new XMLMapping();

            //获取类型的映射信息
            MappingInfo mapInfo = xmlMapping.GetDataMapInfo(entity.GetType());
            Database    db      = new Database();

            db.CommandText           = storageprocedure;
            mapInfo.CurrentOperation = db.CommandText;
            addParameters(db, mapInfo, filterClause, entity);
            IDbCommand command = db.GetCommand();

            command.CommandTimeout = 1000;
            int           result        = -1;
            StringBuilder errorMessages = new StringBuilder();

            try
            {
                result = command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    errorMessages.Append("Index #" + i + "\n" +
                                         "Message: " + ex.Errors[i].Message + "\n" +
                                         "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                                         "Source: " + ex.Errors[i].Source + "\n" +
                                         "Procedure: " + ex.Errors[i].Procedure + "\n");
                }//返回错误信息、行数、来源、程序

                //控制台抛出异常
                throw new Exception(errorMessages.ToString());
                //Console.WriteLine(errorMessages.ToString());
                //Console.ReadLine();
            }
            return(result);
        }