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);

                }

            }


  • 相关阅读:
    Cycle Sort
    使用finalize/dispose 模式提高GC性能(翻译)
    支持在控件标签间包含子控件 WebControl
    MongoDB 使用GridFS保存文件
    MongoDB学习笔记
    跨域名单点登录 part 1 设计蓝图(翻译)
    UserControl 用户自定义控件
    为什么90%的IT人员都不适合做老大?
    什么情况下你会毫不犹豫地辞职?
    Supervisor安装、配置、开启启动
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12351401.html
Copyright © 2011-2022 走看看