zoukankan      html  css  js  c++  java
  • C# 大数据插入代码

    640?wx_fmt=png

     public static 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.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);

                }

            }


  • 相关阅读:
    重写保存按钮save事件
    隐藏列获取不到值,表格选中行提示未选中
    前后台获取上下文context
    editGrid分录表格
    通用查询-高级查询
    js保留位和取整
    在Visual Studio中使用C++创建和使用DLL
    Lua中的一些库(1)
    Lua中的面向对象编程
    Lua中的模块与包
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12351402.html
Copyright © 2011-2022 走看看