コード例 #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);
        }
コード例 #2
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);
        }