转自http://blog.csdn.net/winnyrain/article/details/51240684

Overcome SqlBulkCopy Limitations with C# Bulk Insw3school.com.cnert, Update, Delete and Merge

 
// Support all type of operations
var bulk = new BulkOperation(connection);
bulk.BulkInsert(dt);
bulk.BulkUpdate(dt);
bulk.BulkDelete(dt);
bulk.BulkMerge(dt); // Support List<T> and Lambda Mapping
var bulk = new BulkOperation<Customer>(connection);
bulk.ColumnInputExpression = c => new { c.Name, c.FirstName };
bulk.ColumnOutputExpression = c => c.CustomerID;
bulk.ColumnPrimaryKeyExpression = c => c.Code;
bulk.BulkMerge(customers);

High Performance Operations

Use scalable bulk operations (Bulk Insert, Update, Delete and Merge) and always get the best performance available for your database provider.

  • SQL Server 2008+
  • SQL Azure
  • SQL Compact
  • MySQL
  • SQLite
  • PostgreSQL (Coming soon)
  • Oracle (Coming soon)
Operations 1,000 Rows 10,000 Rows 100,000 Rows 1,000,000 Rows
Insert 6 ms 25 ms 200 ms 2,000 ms
Update 50 ms 80 ms 575 ms 6,500 ms
Delete 45 ms 70 ms 625 ms 6,800 ms
Merge 65 ms 160 ms 1,200 ms 12,000 ms

* Benchmark for SQL Server

Output Identity Value

Overcome SqlBulkCopy limitations and use flexible features to output inserted identity and concurrency column values.

// Output newly inserted identity value after an insert
bulk.ColumnMappings.Add("CustomerID", ColumnMappingDirectionType.Output); bulk.BulkInsert(dt);
// Support all type of operations
var bulk = new BulkOperation(connection);
bulk.BulkInsert(dt);
bulk.BulkUpdate(dt);
bulk.BulkDelete(dt);
bulk.BulkMerge(dt);
bulk.BulkSaveChanges(ds);
bulk.BulkSynchronize(dt);
Reference:
http://bulk-operations.net/
http://www.zzzprojects.com/

Examples:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;
using Z.BulkOperations;
using Z.Data.SqlClient; namespace BulkTest
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.TableName = "OBDData";             DataColumn column = new DataColumn("serviceid", typeof(long));
            column.AutoIncrement = true;
            dt.Columns.Add(column);
            dt.Columns.Add(new DataColumn("gpstime", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("lat", typeof(Decimal)));
            dt.Columns.Add(new DataColumn("lng", typeof(Decimal)));
            dt.Columns.Add(new DataColumn("speed", typeof(Decimal)));
            dt.Columns.Add(new DataColumn("altitude", typeof(int)));             for (int d = 0; d < 100000; d++)
            {
               dt.Rows.Add(new object[] { null, DateTime.Now, d % 100, (d + 2) / (d + 1), (d + 3) / (d + 1), (d + 4) % 8 });
            }
            string ConnectionString = @"server=192.168.20.115\MSSQLSERVER2008;database=GPSTest;uid=test;pwd=test";             Stopwatch sw = new Stopwatch();
            using (DbConnection connection = new SqlConnection(ConnectionString))
            {                 connection.Open();
                sw.Start();
                var bulk = new BulkOperation(connection);
                //bulk.BulkInsert(dt);
                bulk.BulkUpdate(dt);
                //bulk.BulkDelete(dt);
                //bulk.BulkMerge(dt);
                //bulk.BulkSaveChanges(ds);
                //bulk.BulkSynchronize(dt);
                sw.Stop();
                Console.WriteLine("用时:" + sw.ElapsedMilliseconds.ToString());
                Console.Read();             }
        }
    }
}

C# Bulk Operations(转)的更多相关文章

  1. drupal7 Views Bulk Operations (VBO)

    介绍 drupal通常用views制作列表,列表也应该能实现某些操作,例如删除.审批等,并且应该是批量进行的,VBO的存在就是为了实现views批量操作功能.事实上,drupal把操作统称为actio ...

  2. bulk更新mongodb的脚本

    bulk批处理mongodb,比普通的js脚本来的更快一些. 官方网址:https://docs.mongodb.com/manual/reference/method/Bulk/ bulk支持的方法 ...

  3. Java使用实现面向对象编程:第七章集合框架的解读=>重中之重

    对于集合框架,是非常重要的知识,是程序员必须要知道的知识点. 但是我们为什么要引入集合框架呢? 我们之前用过数组存储数据,但是采用数组存储存在了很多的缺陷.而现在我们引用了集合框架,可以完全弥补了数组 ...

  4. ElasticSearch+Kibana 索引操作( 附源码)

    一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elastics ...

  5. jquery1.7.2的源码分析(二)

    jquery.extend jQuery.extend = jQuery.fn.extend = function () { var options, name, src, copy, copyIsA ...

  6. sql跨库查询

    ---------------------------------------------------------------------------------- --1. 创建链接服务器 --1. ...

  7. Backbone源码分析(三)

    Backbone源码分析(一) Backbone源码分析(二) Backbone中主要的业务逻辑位于Model和Collection,上一篇介绍了Backbone中的Model,这篇文章中将主要探讨C ...

  8. jQuery 2.0.3 源码分析 钩子机制 - 属性操作

    jQuery提供了一些快捷函数来对dom对象的属性进行存取操作. 这一部分还是比较简单的. 根据API这章主要是分解5个方法 .attr()   获取匹配的元素集合中的第一个元素的属性的值  或 设置 ...

  9. jQuery 3.0 的 setter/getter 模式

    jQuery 的 setter/getter 共用一个函数,通过是否传参来表明它是何种意义.简单说传参它是 setter,不传它是 getter. 一个函数具有多种意义在编程语言中并不罕见,比如函数重 ...

随机推荐

  1. 使用docker搭建公司redmine服务器

    What is Redmine? Redmine is a flexible project management web application. Written using the Ruby on ...

  2. C# 未能加载文件或程序集“xxx”或它的某一个依赖项。参数错误。(异常来自 HRESULT:0x80070057 (E_INVALIDARG))

    错误信息: 因为电脑突然蓝屏,然后重启,再运行项目,报了这个错. 解决方案: 环境是:VS2012+Win7 通过网上查找, 4.0 删除 C:\Windows\Microsoft.NET\Frame ...

  3. python nose测试框架全面介绍九---各种html报告插件对比

    一直在使用Nose-html-reporting,并输出html报告,但今天在使用时发出有点问题:于时,将python目前可能的html报告插件下载后进行对比,如下 一.Nose-html-repor ...

  4. 对Yii2中 yii\web\User的理解,和自建的app\models\User(基础版),frontend\models\User的应用原理

    yii\web\User 是一个统称,为用户,没有具体实例,只能管理: 此处以app\models\User为基准: app\models\User 是映射数据表user的model类,同时也实现接口 ...

  5. js 的基础知识

    一.弱类型意识 js变量是没有类型的 var  a = 1;  //a 就是一个变量 不要提什么类型 变量可以赋任何类型的值 类型仅仅是值的性质 与变量无关 Js的基本类型 变量未赋值时,其值为und ...

  6. Java--static、final、static final的区别

    一.final final修饰类:表示该类不能被继承:final类中的方法默认是final的: final修饰方法:表示该方法无法被重写: final修饰方法参数:表示在变量的生存期中它的值不能被改变 ...

  7. ubuntu怎么安装下载工具uget+aria2 for firefox

    Windows下的下载工具--迅雷,之所以下载速度快,乃是它能搜索资源.为己所用,而不是仅仅从原始地址这单一资源处下载. Ubuntu下也有类似的工具,那就是aira2. aira2是一个命令行下载工 ...

  8. POJ 2195 - Going Home - [最小费用最大流][MCMF模板]

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...

  9. MySQL复制日常维护与管理

    一.复制一些常见设置 1.mysql复制启动时参数: mysql启动时的参数包括:master_host,master_port,master_user,master_password,master_ ...

  10. android adt自带eclipse无法设置ndk路径

    android sdk官网下载r23版本的adt时自带的eclipse没有设置ndk路径的地方,通过Install New Software 发现无法更新,那么如何解决这个问题呢?     软件百度云 ...