void DoBatch(String action, Func <IAdministrator, Boolean> callback) { Int32[] vs = gvExt.SelectedIntValues; if (vs == null || vs.Length < 1) { return; } Int32 n = 0; IEntityOperate eop = EntityFactory.CreateOperate(EntityType); eop.BeginTransaction(); try { foreach (Int32 item in vs) { IEntity entity = eop.FindByKey(item); IAdministrator admin = entity as IAdministrator; if (admin != null && callback(admin)) { entity.Save(); n++; } } eop.Commit(); WebHelper.Alert("成功" + action + n + "个管理员!"); } catch (Exception ex) { eop.Rollback(); WebHelper.Alert("操作失败!" + ex.Message); } if (n > 0) { gv.DataBind(); } }
void DoBatch(String action, Func <IRole, Boolean> callback) { Int32[] vs = gvExt.SelectedIntValues; if (vs == null || vs.Length < 1) { return; } Int32 n = 0; IEntityOperate eop = Factory; eop.BeginTransaction(); try { foreach (Int32 item in vs) { IRole entity = FindByRoleID(item); if (entity != null && callback(entity)) { entity.Save(); n++; } } eop.Commit(); WebHelper.Alert("成功为" + n + "个部门" + action + "!"); } catch (Exception ex) { eop.Rollback(); WebHelper.Alert("操作失败!" + ex.Message); } if (n > 0) { gv.DataBind(); } }
/// <summary>把整个集合从数据库中删除</summary> /// <param name="useTransition">是否使用事务保护</param> /// <returns></returns> public Int32 Delete(Boolean useTransition) { if (Count < 1) { return(0); } Int32 count = 0; if (useTransition) { IEntityOperate dal = Factory; dal.BeginTransaction(); try { foreach (T item in this) { count += item.Delete(); } dal.Commit(); } catch { dal.Rollback(); throw; } } else { foreach (T item in this) { count += item.Delete(); } } return(count); }
/// <summary>把一个表的数据全部导入到另一个表</summary> /// <param name="eop">实体操作者。</param> /// <param name="count">要迁移的记录数,默认0表示全部</param> /// <param name="isDesc">是否降序。默认升序</param> /// <param name="getData">用于获取数据的委托</param> /// <returns></returns> public Int32 TransformTable(IEntityOperate eop, Int32 count = 0, Boolean?isDesc = null, Func <Int32, Int32, IEntityList> getData = null) { var name = eop.TableName; if (count <= 0) { count = eop.Count; } if (getData == null) { var order = ""; if (isDesc != null) { var fi = eop.Unique; if (fi != null) { order = isDesc.Value ? fi.Desc() : fi.Asc(); } } getData = (start, max) => eop.FindAll(null, order, null, start, max); } // 在目标链接上启用事务保护 eop.ConnName = DesConn; eop.BeginTransaction(); try { XTrace.WriteLine("{0} 共 {1}", name, count); if (OnlyTransformToEmptyTable && eop.Count > 0) { XTrace.WriteLine("{0} 非空,跳过", name); eop.Rollback(); return(0); } // 允许插入自增 var oldII = eop.AllowInsertIdentity; if (AllowInsertIdentity) { eop.AllowInsertIdentity = true; } // 关闭SQL日志 var oldShowSql = DAL.ShowSQL; DAL.ShowSQL = false; var total = 0; var index = 0; while (true) { var size = Math.Min(BatchSize, count - index); if (size <= 0) { break; } eop.ConnName = SrcConn; var list = getData(index, size); if (list == null || list.Count < 1) { break; } index += list.Count; // 处理事件,外部可以修改实体数据 if (OnTransformEntity != null) { var e = new EventArgs <IEntity>(null); foreach (var entity in list) { e.Arg = entity; OnTransformEntity(this, e); } } eop.ConnName = DesConn; var rs = list.Insert(true); XTrace.WriteLine("{0} 导入 {1}/{2} {3:p}", name, index, count, (Double)index / count); total += rs; } DAL.ShowSQL = oldShowSql; // 关闭插入自增 if (AllowInsertIdentity) { eop.AllowInsertIdentity = oldII; } // 在目标链接上启用事务保护 eop.ConnName = DesConn; eop.Commit(); return(total); } catch (Exception ex) { XTrace.WriteLine("{0} 错误 {1}", name, ex.Message); // 在目标链接上启用事务保护 eop.ConnName = DesConn; eop.Rollback(); throw; } }
/// <summary>把一个表的数据全部导入到另一个表</summary> /// <param name="eop">实体操作者。</param> /// <param name="count">要迁移的记录数,默认0表示全部</param> /// <param name="isDesc">是否降序。默认升序</param> /// <param name="getData">用于获取数据的委托</param> /// <returns></returns> public Int32 TransformTable(IEntityOperate eop, Int32 count = 0, Boolean? isDesc = null, Func<Int32, Int32, IEntityList> getData = null) { var name = eop.TableName; if (count <= 0) count = eop.Count; if (getData == null) { var order = ""; if (isDesc != null) { var fi = eop.Unique; if (fi != null) order = isDesc.Value ? fi.Desc() : fi.Asc(); } getData = (start, max) => eop.FindAll(null, order, null, start, max); } // 在目标链接上启用事务保护 eop.ConnName = DesConn; eop.BeginTransaction(); try { XTrace.WriteLine("{0} 共 {1}", name, count); if (OnlyTransformToEmptyTable && eop.Count > 0) { XTrace.WriteLine("{0} 非空,跳过", name); eop.Rollback(); return 0; } // 允许插入自增 var oldII = eop.AllowInsertIdentity; if (AllowInsertIdentity) eop.AllowInsertIdentity = true; // 关闭SQL日志 var oldShowSql = DAL.ShowSQL; DAL.ShowSQL = false; var total = 0; var index = 0; while (true) { var size = Math.Min(BatchSize, count - index); if (size <= 0) break; eop.ConnName = SrcConn; var list = getData(index, size); if (list == null || list.Count < 1) break; index += list.Count; // 处理事件,外部可以修改实体数据 if (OnTransformEntity != null) { var e = new EventArgs<IEntity>(null); foreach (var entity in list) { e.Arg = entity; OnTransformEntity(this, e); } } eop.ConnName = DesConn; var rs = list.Insert(true); XTrace.WriteLine("{0} 导入 {1}/{2} {3:p}", name, index, count, (Double)index / count); total += rs; } DAL.ShowSQL = oldShowSql; // 关闭插入自增 if (AllowInsertIdentity) eop.AllowInsertIdentity = oldII; // 在目标链接上启用事务保护 eop.ConnName = DesConn; eop.Commit(); return total; } catch (Exception ex) { XTrace.WriteLine("{0} 错误 {1}", name, ex.Message); // 在目标链接上启用事务保护 eop.ConnName = DesConn; eop.Rollback(); throw; } }