/// <summary> /// 执行返回一个DataSet集合。 /// </summary> /// <param name="db">操作数据库对象</param> /// <param name="dbCmd">数据操作的dbCommand</param> /// <param name="transaction"></param> /// <returns>>DataSet 数据格式的 数据集</returns> public DataSet ExecuteDataSet(Database db, DbCommand dbCmd, DbTransaction transaction) { DataSet ds = new DataSet(); int rowCounts = -1; try { string cmdMsg = MB.Orm.Persistence.DbCommandExecuteTrack.Instance.CommandToTrackMessage(db, dbCmd); MB.Util.TraceEx.Write("正在执行:" + cmdMsg); //设定查询行为 var qh = _QueryBehavior; if (qh == null) { qh = MessageHeaderHelper.GetQueryBehavior(); } if (qh != null && qh.IsQueryAll) { qh = null; //如果允许查询全部,则给出全部值 } //增加动态列 2012-08-21 add by aifang begin resetDynamicColumns(dbCmd, qh); //增加动态列 2012-08-21 add by aifang end using (new Util.MethodTraceWithTime(true, cmdMsg)) { bool isTotolPageDisplayed = (qh == null ? false : qh.IsTotalPageDisplayed); if (isTotolPageDisplayed) { DataSet dsTemp = new DataSet(); dsTemp = transaction == null?db.ExecuteDataSet(dbCmd) : db.ExecuteDataSet(dbCmd, transaction); if (dsTemp.Tables.Count > 0) { //在这种情况下,返回的是全部记录数的数量 rowCounts = dsTemp.Tables[0].Rows.Count; } if (qh != null) { ds = dsTemp.Clone(); int start = qh.PageIndex * qh.PageSize; for (int i = 0; i < rowCounts; i++) { //小于起始位置的行 if (i < start) { continue; } //大于当前页的最大记录 if (i >= (start + qh.PageSize)) { break; } DataRow dr = ds.Tables[0].NewRow(); for (int colIndex = 0; colIndex < ds.Tables[0].Columns.Count; colIndex++) { dr[colIndex] = dsTemp.Tables[0].Rows[i][colIndex]; } ds.Tables[0].Rows.Add(dr); } } else { ds = dsTemp; } } else { if (qh != null) { int start = qh.PageIndex * qh.PageSize; int returnedCount = 0; if (qh.PageSize < Int32.MaxValue) { //这里判断一下,是否是INT32的最大值,以防止整数的溢出 returnedCount = qh.PageSize + 1; ds = db.ExecuteDataSet(dbCmd, transaction, start, returnedCount); } else { ds = db.ExecuteDataSet(dbCmd, transaction, start, qh.PageSize); } //在这里需要多返回一条记录,客户端用来判断是否有下一页 if (ds.Tables.Count > 0) { rowCounts = ds.Tables[0].Rows.Count; //清除多返回的记录 if (returnedCount > 0 && rowCounts == returnedCount) { ds.Tables[0].Rows.RemoveAt(rowCounts - 1); } } } else { ds = transaction == null?db.ExecuteDataSet(dbCmd) : db.ExecuteDataSet(dbCmd, transaction); if (ds.Tables.Count > 0) { rowCounts = ds.Tables[0].Rows.Count; //这里返回的也是全部记录 } } } } if (qh != null && qh.PageSize != Int32.MaxValue) { //如果有些语句不想分页的,要么不设置QueryBehavior或者把PageSize设置为MaxValue,则不传分页信息到客户端 MessageHeaderHelper.AppendMessageHeaderResponse(new ResponseHeaderInfo(rowCounts), true); DataBaseQueryResult.Instance.SetTotalRows(rowCounts); } } catch (Exception ex) { throw new MB.RuleBase.Exceptions.DatabaseExecuteException(" 数据执行SQL 语句 出错!", ex); } finally { dbCmd.Dispose(); } return(ds); }
/// <summary> /// 根据对象类型获取值 /// </summary> /// <typeparam name="T">泛型对象</typeparam> /// <param name="entityType">泛型对象的类型,为了兼容以前的接口才这么做</param> /// <param name="transaction">事务</param> /// <param name="db">数据库对象</param> /// <param name="cmd">数据库命令</param> /// <returns>对象集合</returns> public List <T> GetObjects <T>(Type entityType, DbTransaction transaction, Database db, DbCommand cmd) { if (entityType.IsValueType || entityType.Equals(typeof(string))) { return(GetValueTypeObjects <T>(entityType, transaction, db, cmd)); } bool isBaseModelSub = entityType.IsSubclassOf(typeof(BaseModel)); var attMapping = MB.Orm.Mapping.AttMappingManager.Instance; var lstMapping = attMapping.CheckExistsModelMapping(entityType) ? attMapping.GetModelMappingInfo(entityType).FieldPropertys.Values.ToList() : attMapping.GetEntityMappingPropertys(entityType); List <T> ic = new List <T>(); var qh = _QueryBehavior; if (qh == null) { qh = MessageHeaderHelper.GetQueryBehavior(); } resetDynamicColumns(cmd, qh); string cmdMsg = MB.Orm.Persistence.DbCommandExecuteTrack.Instance.CommandToTrackMessage(db, cmd); MB.Util.TraceEx.Write("正在执行:" + cmdMsg); using (new Util.MethodTraceWithTime(true, cmdMsg)) { using (IDataReader reader = transaction == null ? db.ExecuteReader(cmd) : db.ExecuteReader(cmd, transaction)) { try { DataTable dtReader = reader.GetSchemaTable(); Dictionary <string, MB.Util.Emit.DynamicPropertyAccessor> existsFields = new Dictionary <string, MB.Util.Emit.DynamicPropertyAccessor>(); foreach (MB.Orm.Mapping.FieldPropertyInfo proInfo in lstMapping) { if (dtReader.Select(string.Format("ColumnName='{0}'", proInfo.FieldName)).Length <= 0) { continue; } System.Reflection.PropertyInfo propertyInfo = entityType.GetProperty(proInfo.PropertyName); if (propertyInfo == null) { continue; } MB.Util.Emit.DynamicPropertyAccessor propertyAccess = new MB.Util.Emit.DynamicPropertyAccessor(entityType, propertyInfo); existsFields.Add(proInfo.FieldName, propertyAccess); } if (existsFields.Count > 0) { int rows = -1; int firstIndex = qh != null ? qh.PageIndex * qh.PageSize : -1; while (reader.Read()) { rows += 1; //限制读取的最大行数 if (qh != null) { if (rows < firstIndex) { continue; } if ((rows - firstIndex) >= qh.PageSize) { int currentRowsCount = rows - firstIndex; //如果读取的记录数大于当前页规定的记录数,如果要返回全部记录,则游标继续,如果不要返回,则推出循环 if (qh.IsTotalPageDisplayed) { continue; } else if (currentRowsCount == qh.PageSize) //在服务器这边多读取一条,以通知客户端是否需要分页 { continue; } else { break; } } } T entity = (T)MB.Util.DllFactory.Instance.CreateInstance(entityType); foreach (var proInfo in existsFields) { var val = reader[proInfo.Key]; if (val != null && val != System.DBNull.Value) { existsFields[proInfo.Key].Set(entity, val); } } if (isBaseModelSub) { MB.Orm.Common.BaseModel temp = entity as MB.Orm.Common.BaseModel; temp.EntityState = EntityState.Persistent; } ic.Add(entity); } if (qh != null && qh.PageSize < Int32.MaxValue) { //如果有些语句不想分页的,要么不设置QueryBehavior或者把PageSize设置为MaxValue,则不传分页信息到客户端 MessageHeaderHelper.AppendMessageHeaderResponse(new ResponseHeaderInfo(rows + 1), true); DataBaseQueryResult.Instance.SetTotalRows(rows + 1); } } } catch (Exception ex) { throw new MB.RuleBase.Exceptions.DatabaseExecuteException("执行GetObjectsByXml<" + entityType.FullName + "> 出错!", ex); } finally { cmd.Dispose(); } } } return(ic); }