zoukankan      html  css  js  c++  java
  • 批量插入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);
                }
            } 
  • 相关阅读:
    vue中element-ui的内置过渡动画
    vue路由自动加载、按组件异步载入vuex以及dll优化
    h5春节小游戏制作
    python脚本通过adb命令安装包
    pipeline
    python闭包与装饰器
    linux-awk
    CSAPP Lab3: The Attack Lab
    CSAPP Lab4 Cache Lab
    HDU. 2243. 考研路茫茫——单词情结(AC自动机 DP 矩阵快速幂)
  • 原文地址:https://www.cnblogs.com/xuyufeng/p/9907964.html
Copyright © 2011-2022 走看看