示例#1
0
        /// <summary>
        /// 对数据的批量更新
        /// </summary>
        /// <param name="tableName">目标表格名称</param>
        /// <param name="startId">当前更新记录开始位置(不包括所传入的id)</param>
        /// <param name="updateType">更新类型枚举</param>
        public static void BulkUpdate(string tableName, int startId, DataUpdateType updateType = DataUpdateType.Insert)
        {
            var bllEntity = BllFactory.GetBllInstance(tableName);
            var bllType   = bllEntity.GetType();

            try
            {
                var whereStr = "Id>" + startId;
                var idList   = (IEnumerable <object>)bllType.InvokeMember("QuerySingleColumn", BindingFlags.InvokeMethod, null, bllEntity, new object[] { whereStr, "Id" });

                var updateModelList = idList.Select(id => new DbUpdateLog
                {
                    TableName  = tableName,
                    TargetId   = (int)id,
                    UpdateType = (int)updateType,
                    UpdateTime = DateTime.Now
                });

                UpdateBll.BulkInsert(updateModelList);
            }
            catch (Exception ex)
            {
                ExceptionLogBll.ExceptionPersistence("DataUpdateLog.cs", "DataUpdateLog", ex);
            }
        }
示例#2
0
        private void Persistent(List <Posts> posts, List <DepartInfo> departs, List <PersonInfo> staff)
        {
            var staffBll    = new PersonInfoBll();
            var dbUpdateBll = new DbUpdateLogBll();

            Func <bool>[] delegates = new Func <bool> [3];
            delegates[0] = () =>
            {
                if (posts.Count > 0)
                {
                    var postBll = new PostsBll();
                    var maxId   = postBll.GetMaxId();
                    postBll.BulkInsert(posts);

                    var ids    = postBll.QueryList("Id>" + maxId, new[] { nameof(Posts.Id) }).Select(p => p.Id);
                    var dbLogs = ids.Select(id => new DbUpdateLog {
                        TableName = nameof(Posts), TargetId = id, UpdateType = 1, UpdateTime = DateTime.Now
                    });
                    dbUpdateBll.BulkInsert(dbLogs);
                }
                return(true);
            };
            delegates[1] = () =>
            {
                if (departs.Count > 0)
                {
                    var departBll = new DepartInfoBll();
                    var maxId     = departBll.GetMaxId();
                    departBll.BulkInsert(departs);

                    var ids    = departBll.QueryList("Id>" + maxId, new[] { nameof(DepartInfo.Id) }).Select(p => p.Id);
                    var dbLogs = ids.Select(id => new DbUpdateLog {
                        TableName = nameof(DepartInfo), TargetId = id, UpdateType = 1, UpdateTime = DateTime.Now
                    });
                    dbUpdateBll.BulkInsert(dbLogs);
                }
                return(true);
            };
            delegates[2] = () =>
            {
                List <Posts>      _posts   = new PostsBll().QueryAll().ToList();
                List <DepartInfo> _departs = new DepartInfoBll().QueryAll().ToList();
                staff.ForEach(s =>
                {
                    // 根据前面用 PersonId 存储的部门名称找到此员工对应的部门
                    // 根据前面用 PhotoPath 存储的职务名称找到此员工对应的职务
                    var depart     = _departs.Find(d => d.DepartmentName == s.PersonId);
                    var post       = _posts.Find(p => p.PostName == s.PhotoPath);
                    s.DepartmentId = depart?.Id ?? 0;
                    s.PostId       = post?.Id ?? 0;
                    s.PersonId     = string.Empty;
                    s.PhotoPath    = string.Empty;
                });

                var maxId = staffBll.GetMaxId();
                staffBll.BulkInsert(staff);

                var ids    = staffBll.QueryList("Id>" + maxId, new[] { nameof(PersonInfo.Id) }).Select(p => p.Id);
                var dbLogs = ids.Select(id => new DbUpdateLog {
                    TableName = nameof(PersonInfo), TargetId = (int)id, UpdateType = 1, UpdateTime = DateTime.Now
                });
                dbUpdateBll.BulkInsert(dbLogs);

                return(true);
            };

            // 若插入失败,则尝试五次
            for (var i = 0; i < 5; i++)
            {
                var success = staffBll.ExecuteTransation(delegates);
                if (success)
                {
                    return;
                }
            }
        }