• EF 批量插入,sqlhelper 批量插入


    需添加一个using System.Linq; 引用

    public void BulkInsert<T>(string connection, string tableName, IList<T> list) { using (var bulkCopy = new SqlBulkCopy(connection)) { bulkCopy.BatchSize = list.Count; bulkCopy.DestinationTableName = tableName; var table = new DataTable(); var props = TypeDescriptor.GetProperties(typeof(T)) .Cast<PropertyDescriptor>() .Where(propertyInfo => propertyInfo.PropertyType != null && propertyInfo.PropertyType.Namespace != null && propertyInfo.PropertyType.Namespace.Equals("System")) .ToArray(); foreach (var propertyInfo in props) { bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name); table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType); } var values = new object[props.Length]; foreach (var item in list) { for (var i = 0; i < values.Length; i++) { values[i] = props[i].GetValue(item); } table.Rows.Add(values); } bulkCopy.WriteToServer(table); } }
  • 相关阅读:
    Weex-语法笔记 一
    Entitlements (授权机制) 延伸
    FOUNDATION_EXPORT 延伸
    NS_ASSUME_NONNULL_BEGIN 延伸
    SSLPinning 延伸
    Socket.io 延伸
    UI_APPEARANCE_SELECTOR 延伸
    Modules和Autolinking
    代码块(Block)回调一般阐述
    数据库连接池配置错误导致OOM
  • 原文地址:https://www.cnblogs.com/yiliuyang/p/5591155.html
Copyright © 2020-2023  润新知