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()}");
                }
            }
  • 相关阅读:
    java接口请求超时处理方法
    Spring Cloud GateWay 服务网关
    Mysql中on条件和where条件的使用总结
    Elasticsearch之javaAqi
    Elasticsearch—CRUD
    ElasticSearch的版本控制和Mapping创建
    cmd定时任务计划
    CSS简介及使用
    html简介及应用
    Python基础(十五)-IO模型
  • 原文地址:https://www.cnblogs.com/macT/p/13575502.html
Copyright © 2011-2022 走看看