• 批量插入bulkcopy


     public static void InsertBatch<T>(IDbConnection conn, IEnumerable<T> entityList, string tblName, IDbTransaction transaction = null) where T : class
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                using (var bulkCopy = new SqlBulkCopy(conn as SqlConnection))
                {
                    var enumerable = entityList as T[] ?? entityList.ToArray();
                    bulkCopy.BatchSize = enumerable.Count();
                    bulkCopy.DestinationTableName = tblName;
                    var table = new DataTable();
                    DapperExtensions.Sql.ISqlGenerator sqlGenerator = new SqlGeneratorImpl(new DapperExtensionsConfiguration());
                    var classMap = sqlGenerator.Configuration.GetMap<T>();
                    IPropertyMap[] props;
                    if (tblName == "RepayRecords")
                    {
                        props = classMap.Properties.Where(x => x.Ignored == false && x.ColumnName != "DeductWays").ToArray();
                    }
                    else
                    {
                        props = classMap.Properties.Where(x => x.Ignored == false).ToArray();
                    }

                    foreach (var propertyInfo in props)
                    {
                        bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
                        table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyInfo.PropertyType) ?? propertyInfo.PropertyInfo.PropertyType);
                    }
                    var values = new object[props.Count()];
                    foreach (var itemm in enumerable)
                    {
                        for (var i = 0; i < values.Length; i++)
                        {
                            values[i] = props[i].PropertyInfo.GetValue(itemm, null);
                        }
                        table.Rows.Add(values);
                    }
                    bulkCopy.WriteToServer(table);
                }
            } 
  • 相关阅读:
    Anaconda使用命令
    排序算法3--插入排序--希尔排序(缩小增量排序)
    排序算法2--插入排序--折半插入排序
    排序算法1--插入排序--直接插入排序
    排序总结---常用的排序算法总结,java和js实现
    前端兼容性问题
    js对象的几种创建方式和js实现继承的方式[转]
    js原型和原型链[转]
    性能优化的方法
    Http状态码
  • 原文地址:https://www.cnblogs.com/xuyufeng/p/9907964.html
Copyright © 2020-2023  润新知