• SqlBulkCopy 帮助类


    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;

    namespace Common
    {
        public class BulkInsert
        {
            private string connectionString;


            public BulkInsert(string _con)
            {
                connectionString = ConfigurationManager.ConnectionStrings[_con].ConnectionString;
            }

            public BulkInsert(string _con, bool DESE)
            {
                connectionString = DESEncrypt.Decrypt(ConfigurationManager.ConnectionStrings[_con].ConnectionString);
            }


            public void WriteToServer<T>(List<T> list, string TableName)
            {

                using (SqlConnection destinationConnection = new SqlConnection(connectionString))
                {


                    DataTable dt = list.ToDataTable();

                    destinationConnection.Open();
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
                    {
                        bulkCopy.DestinationTableName = TableName;
                        bulkCopy.NotifyAfter = 30000;
                        try
                        {
                            bulkCopy.WriteToServer(dt);

                            Console.WriteLine("{0}插入{1}条数据", TableName, list.Count);
                        }
                        catch (Exception ex)
                        {
                            //Console.WriteLine(ex.Message);
                            throw ex;
                        }
                        finally
                        {

                        }
                    }
                }

            }

        }

        public static class DataTableHelper
        {

            public static DataTable ToDataTable<T>(this List<T> list)
            {

                //创建属性的集合   
                List<PropertyInfo> pList = new List<PropertyInfo>();
                //获得反射的入口   

                Type type = typeof(T);
                DataTable dt = new DataTable();

                //把所有的public属性加入到集合 并添加DataTable的列   
                Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
                foreach (var item in list)
                {
                    //创建一个DataRow实例   
                    DataRow row = dt.NewRow();
                    //给row 赋值   
                    pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
                    //加入到DataTable   
                    dt.Rows.Add(row);
                }
                return dt;
            }

            public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
            {
                return source.Distinct(new CommonEqualityComparer<T, V>(keySelector));
            }
        }
        public class CommonEqualityComparer<T, V> : IEqualityComparer<T>
        {
            private Func<T, V> keySelector;

            public CommonEqualityComparer(Func<T, V> keySelector)
            {
                this.keySelector = keySelector;
            }

            public bool Equals(T x, T y)
            {
                return EqualityComparer<V>.Default.Equals(keySelector(x), keySelector(y));
            }

            public int GetHashCode(T obj)
            {
                return EqualityComparer<V>.Default.GetHashCode(keySelector(obj));
            }
        }
    }

    代码改变世界
  • 相关阅读:
    CSS实现文字上标、下标
    Inellij idea创建javaWeb以及Servlet简单实现
    利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解
    IntelliJ IDEA使用教程一 介绍&安装&配置
    JavaScript定时器实现的原理分析
    将 Django 应用程序部署到生产服务器
    html中的table导出Excel (亲测有用(●'◡'●))
    HTML用JS导出Excel的五种方法
    JS 导出网页中Table内容到excel
    Python数据库连接池实例——PooledDB
  • 原文地址:https://www.cnblogs.com/oreobyzf/p/5933704.html
Copyright © 2020-2023  润新知