//把ListData 中的所有实体对象 转换为 可执行的DataTable 格式 private DataTable convertToDataTable(Database db, string tableName, MB.Orm.DbSql.SqlString sqlStr, IList lstData) { IList <SqlParamInfo> tPars = sqlStr.ParamFields; DataTable dTable = createTable(tableName); Dictionary <string, MB.Util.Emit.DynamicPropertyAccessor> dynamicAcc = new Dictionary <string, MB.Util.Emit.DynamicPropertyAccessor>(); object entityData = lstData[0]; Type objType = entityData.GetType(); //edit by cdc 2011-3-13 if (objType.IsValueType || string.Compare(objType.Name, "String", true) == 0) { if (tPars.Count != 1) { throw new MB.Util.APPException(string.Format("批量处理中,值只有一个而参数有{0}个", tPars.Count), Util.APPMessageType.SysErrInfo); } var sp = tPars[0]; foreach (var d in lstData) { DataRow newDr = dTable.NewRow(); newDr[sp.MappingName] = d; dTable.Rows.Add(newDr); } } else if ((entityData as DataRow) != null) { foreach (DataRow dr in lstData) { DataRow newDr = dTable.NewRow(); foreach (SqlParamInfo info in tPars) { newDr[info.MappingName] = dr[info.MappingName]; } dTable.Rows.Add(newDr); } } else { foreach (SqlParamInfo info in tPars) { MB.Util.Emit.DynamicPropertyAccessor acc = new MB.Util.Emit.DynamicPropertyAccessor(entityData.GetType(), info.MappingName); dynamicAcc.Add(info.MappingName, acc); } foreach (object entity in lstData) { DataRow newDr = dTable.NewRow(); foreach (string fieldName in dynamicAcc.Keys) { //MB.Util.MyReflection.Instance.ConvertValueType( newDr[fieldName] = dynamicAcc[fieldName].Get(entity); } dTable.Rows.Add(newDr); } } return(dTable); }
//根据参数和实体对象构造出参数值集合。 private Dictionary <string, ArrayList> paramValues(Database db, IList <SqlParamInfo> tPars, IList lstData) { Dictionary <string, ArrayList> values = new Dictionary <string, ArrayList>(); object obj = lstData[0]; Type objType = obj.GetType(); //edit by cdc 2011-3-13 if (objType.IsValueType || string.Compare(objType.Name, "String", true) == 0) { if (tPars.Count != 1) { throw new MB.Util.APPException(string.Format("批量处理中,值只有一个而参数有{0}个", tPars.Count), Util.APPMessageType.SysErrInfo); } ArrayList lst = new ArrayList(); var sp = tPars[0]; foreach (var d in lstData) { lst.Add(convertToRealDbValue(d, sp.DbType)); } values.Add(sp.Name, lst); } else if ((obj as DataRow) != null) //判断传入的是否为DataRow. { foreach (SqlParamInfo info in tPars) { ArrayList lst = new ArrayList(); foreach (DataRow dr in lstData) { object val = dr[info.MappingName]; lst.Add(convertToRealDbValue(val, info.DbType)); } values.Add(info.Name, lst); } } else { foreach (SqlParamInfo info in tPars) { MB.Util.Emit.DynamicPropertyAccessor acc = new MB.Util.Emit.DynamicPropertyAccessor(objType, info.MappingName); if (acc == null) { throw new MB.Util.APPException(string.Format("配置的属性{0} 在类型{1} 中不存在", info.MappingName, objType.FullName), Util.APPMessageType.SysErrInfo); } ArrayList lst = new ArrayList(); foreach (object entity in lstData) { object val = acc.Get(entity); lst.Add(convertToRealDbValue(val, info.DbType)); } values.Add(info.Name, lst); } } return(values); }
private void button1_Click(object sender, EventArgs e) { EmitProductInfo info = new EmitProductInfo(); MB.Util.Emit.DynamicPropertyAccessor begin = new MB.Util.Emit.DynamicPropertyAccessor(typeof(EmitProductInfo), "BeginDate"); begin.Set(info, null); MB.Util.Emit.DynamicPropertyAccessor datas = new MB.Util.Emit.DynamicPropertyAccessor(typeof(EmitProductInfo), "Datas"); byte[] bts = new byte[] { 243, 34, 234 }; datas.Set(info, bts); MB.Util.Emit.DynamicPropertyAccessor price = new MB.Util.Emit.DynamicPropertyAccessor(typeof(EmitProductInfo), "Price"); price.Set(info, 234); MB.Util.Emit.DynamicPropertyAccessor isStart = new MB.Util.Emit.DynamicPropertyAccessor(typeof(EmitProductInfo), "IsStart"); isStart.Set(info, null); string name = bts.GetType().Name; MessageBox.Show("OK" + info.IsStart.ToString()); }
private static void iniDataTypeProAccess(Type dataType, string proName) { Dictionary <string, MB.Util.Emit.DynamicPropertyAccessor> existsFields = null; if (_DynamicAccess.ContainsKey(dataType)) { existsFields = _DynamicAccess[dataType]; } else { existsFields = new Dictionary <string, MB.Util.Emit.DynamicPropertyAccessor>(); _DynamicAccess[dataType] = existsFields; } if (!existsFields.ContainsKey(proName)) { var pro = dataType.GetProperty(proName); if (pro == null) { throw new MB.Util.APPException(string.Format("实体类型{0} 中不包含键名称 {1}", dataType.FullName, proName), MB.Util.APPMessageType.SysErrInfo); } MB.Util.Emit.DynamicPropertyAccessor propertyAccess = new MB.Util.Emit.DynamicPropertyAccessor(dataType, pro); existsFields[pro.Name] = propertyAccess; } }
/// <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); }
/// <summary> /// 把数据实体集合类转换为 客户可分析DataSet 的格式。 /// </summary> /// <param name="entitys"></param> /// <param name="propertys"></param> /// <param name="columnsEdit"></param> /// <returns></returns> public DataSet ConvertEntityToDataSet(System.Collections.IList entitys, Dictionary <string, ColumnPropertyInfo> propertys, Dictionary <string, ColumnEditCfgInfo> columnsEdit, bool isForChart) { if (entitys == null || propertys == null) { return(null); } try { DataTable dtData = new DataTable(); List <ColumnPropertyInfo> cols = new List <ColumnPropertyInfo>(); foreach (ColumnPropertyInfo info in propertys.Values) { if (isForChart) { if (string.IsNullOrEmpty(info.ChartDataType)) { continue; } } string dataType = info.DataType; if (columnsEdit != null && columnsEdit.ContainsKey(info.Name)) { dataType = "System.String"; } cols.Add(info); dtData.Columns.Add(info.Name, MB.Util.General.CreateSystemType(dataType, false)); } DataSet dsData = new DataSet(); dsData.Tables.Add(dtData); if (entitys.Count == 0 || cols.Count == 0) { return(dsData); } Dictionary <string, MB.Util.Emit.DynamicPropertyAccessor> entityExistsPros = new Dictionary <string, MB.Util.Emit.DynamicPropertyAccessor>(); object tempEntity = entitys[0]; foreach (ColumnPropertyInfo info in cols) { if (MB.Util.MyReflection.Instance.CheckObjectExistsProperty(tempEntity, info.Name)) { if (entityExistsPros.ContainsKey(info.Name)) { continue; } MB.Util.Emit.DynamicPropertyAccessor propertyAccess = new MB.Util.Emit.DynamicPropertyAccessor(tempEntity.GetType(), info.Name); entityExistsPros.Add(info.Name, propertyAccess); // entityExistsPros.Add(info.Name); } } Dictionary <string, Dictionary <string, string> > lookUpDataSource = new Dictionary <string, Dictionary <string, string> >(); foreach (object entity in entitys) { DataRow drNew = dtData.NewRow(); dtData.Rows.Add(drNew); foreach (string columnName in entityExistsPros.Keys) { object val = entityExistsPros[columnName].Get(entity);// MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, columnName); if (val == null || val == System.DBNull.Value) { continue; } if (columnsEdit != null && columnsEdit.ContainsKey(columnName)) { drNew[columnName] = convertValueToDescription(val, columnsEdit[columnName], lookUpDataSource); } else { drNew[columnName] = MB.Util.MyConvert.Instance.ChangeType(val, dtData.Columns[columnName].DataType); } } } return(dsData); } catch (Exception ex) { throw new MB.Util.APPException("执行 ConvertEntityToDataSet 出错" + ex.Message); } }