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

                }

            }


  • 相关阅读:
    vmware ubuntu 异常关机无法连接到网络
    Speed up GCC link
    常用的一些解压命令
    Log4j 漏洞复现
    Test Case Design method Boundary value analysis and Equivalence partitioning
    CCA (Citrix Certified Administrator) exam of “Implementing Citrix XenDesktop 4”
    What is Key Word driven Testing?
    SAP AGS面试小结
    腾讯2013终端实习生一面
    指针的引用
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12351402.html
Copyright © 2011-2022 走看看