通用权限管理系统提供了一套代码生成器,这里介绍少如何使用其中的记录批量修改功能,
方法一:
List<KeyValuePair<string, object>> whereParameters = new List<KeyValuePair<string, object>>(); whereParameters.Add(new KeyValuePair<string, object>(QUOTEntity.FieldSITE_ID, targetSiteId)); whereParameters.Add(new KeyValuePair<string, object>(QUOTEntity.FieldFEE_TYPE, targetFeeType)); List<QUOTEntity> priceList = QUOTManager.GetList<QUOTEntity>(whereParameters); foreach (var PntEntity in priceList) { PntEntity.DeletionStateCode = 1; PntEntity.ModifiedBy = userInfo.RealName; PntEntity.ModifiedOn = DateTime.Now; PntEntity.ModifiedUserId = userInfo.Id; QUOTManager.Update(PntEntity); }
以上方法需要找出全部记录,逐条更新,效率不是很好。
方法二:
List<KeyValuePair<string, object>> whereParameters = new List<KeyValuePair<string, object>>(); whereParameters.Add(new KeyValuePair<string, object>(QUOTEntity.FieldSITE_ID, targetSiteId)); whereParameters.Add(new KeyValuePair<string, object>(QUOTEntity.FieldFEE_TYPE, targetFeeType)); List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>(); parameters.Add(new KeyValuePair<string, object>(QUOTEntity.FieldDeletionStateCode, 1)); parameters.Add(new KeyValuePair<string, object>(QUOTEntity.FieldModifiedBy, userInfo.RealName)); parameters.Add(new KeyValuePair<string, object>(QUOTEntity.FieldModifiedOn, DateTime.Now)); parameters.Add(new KeyValuePair<string, object>(QUOTEntity.FieldModifiedUserId, userInfo.Id)); parameters.Add(new KeyValuePair<string, object>(QUOTEntity.FieldMODIFIEDSITE, userInfo.CompanyName)); QUOTManager.SetProperty(whereParameters, parameters);
可以看到,上面代码使用了SetProperty方法,whereParameters是条件,parameters是参数,
表示将符合条件的记录设置对应的属性。效率要比第一条高。
关于第二种方法,我们来看下其实现
public virtual int SetProperty(KeyValuePair<string, object> whereParameter, KeyValuePair<string, object> parameter) { List<KeyValuePair<string, object>> whereParameters = new List<KeyValuePair<string, object>>(); whereParameters.Add(whereParameter); List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>(); parameters.Add(parameter); return DbLogic.SetProperty(DbHelper, this.CurrentTableName, whereParameters, parameters); }
/// <summary> /// 设置属性 /// </summary> /// <param name="dbHelper">数据库连接</param> /// <param name="tableName">目标表名</param> /// <param name="whereParameters">条件字段,条件值</param> /// <param name="parameters">更新字段,更新值</param> /// <returns>影响行数</returns> public static int SetProperty(IDbHelper dbHelper, string tableName, List<KeyValuePair<string, object>> whereParameters, List<KeyValuePair<string, object>> parameters) { SQLBuilder sqlBuilder = new SQLBuilder(dbHelper); sqlBuilder.BeginUpdate(tableName); foreach (var parameter in parameters) { sqlBuilder.SetValue(parameter.Key, parameter.Value); } sqlBuilder.SetWhere(whereParameters); // sqlBuilder.SetDBNow(FieldModifiedOn); return sqlBuilder.EndUpdate(); }
底层自动实现了条件和设置功能的拼接,满足外部多种调用方式。其底层实现所在位置:
感兴趣的朋友可继续看下其底层实现原理,自从使用第二种方式对记录批量更新后,后台处理速度确实提高了很多。
时间: 2024-10-17 04:46:04