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);
                }
            } 
  • 相关阅读:
    SCRIPT7002: XMLHttpRequest: 网络错误 0x2efe, 由于出现错误 00002efe 而导致此项操作无法完成
    经纬转换成point的sql
    build.xml
    ubuntu下安装vsftpd及vsftpd配置文件不见的解决办法
    500 OOPS: could not read chroot() list file:/etc/vsftpd.chroot_list
    【linux】su、sudo、sudo su、sudo -i的用法和区别
    Js获取上一月份
    BigDecimal工具类
    查询重复的记录
    Excel 合并单元格
  • 原文地址:https://www.cnblogs.com/xuyufeng/p/9907964.html
Copyright © 2011-2022 走看看