zoukankan      html  css  js  c++  java
  • 当提交的数据量较大分批次插入数据

     private void InsertData(List<special_alarm_detour> list)
            {
                try
                {
                    using (var db = new DapperHelper(BasicInformation.connection))
                    {
                        try
                        {
                            List<List<special_alarm_detour>> listGroup = new List<List<special_alarm_detour>>();
                            int j = 1000;
                            for (int i = 0; i < list.Count; i += 1000)//以1000为一组分组
                            {
                                List<special_alarm_detour> cList = new List<special_alarm_detour>();
                                cList = list.Take(j).Skip(i).ToList();
                                j += 1000;
                                listGroup.Add(cList);
                            }
                            for (int i = 0; i < listGroup.Count; i++)
                            {
                                List<special_alarm_detour> fzlist = new List<special_alarm_detour>();
                                fzlist = listGroup[i];
                                using (var ctx = db.TranStart())
                                {
                                    try
                                    {
                                        Stopwatch stopwatch = new Stopwatch();
                                        stopwatch.Restart();
                                        db.Insert<special_alarm_detour>(fzlist);
                                        ctx.Commit();
                                        stopwatch.Stop();
                                        Log.Blue($"【车辆绕行】插入数据:{fzlist.Count}条,耗时:{stopwatch.ElapsedMilliseconds}毫秒");
                                    }
                                    catch (Exception ex)
                                    {
                                        ctx.Rollback();
                                        Log.Error($"【车辆绕行】插入数据报错:{ex.Message.ToString()}");
                                    }
                                }
    
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error($"【车辆绕行】插入数据报错:{ex.Message.ToString()}");
                        }
    
                    }
                }
                catch (Exception ex)
                {
                    Log.Error($"【车辆绕行】插入数据报错:{ex.Message.ToString()}");
                }
            }
  • 相关阅读:
    Python异常处理
    Python序列化中json模块和pickle模块
    Python常用模块random/time/sys/os模块
    软件测试--读书笔记
    团队作业——系统设计和任务分配
    结对项目之需求分析与原型设计
    生成小学计算题(升级版)
    生成小学计算题
    软件工程基础
    第一个微信小项目
  • 原文地址:https://www.cnblogs.com/macT/p/13575502.html
Copyright © 2011-2022 走看看