SqlBulkCopy 快速插入数据
- using System.Data.SqlClient;
- class Program
- {
- static void Main()
- {
- string connectionString = GetConnectionString();
- // Open a sourceConnection to the AdventureWorks database.
- using (SqlConnection sourceConnection =
- new SqlConnection(connectionString))
- {
- sourceConnection.Open();
- // Perform an initial count on the destination table.
- SqlCommand commandRowCount = new SqlCommand(
- "SELECT COUNT(*) FROM " +
- "dbo.BulkCopyDemoMatchingColumns;",
- sourceConnection);
- long countStart = System.Convert.ToInt32(
- commandRowCount.ExecuteScalar());
- Console.WriteLine("Starting row count = {0}", countStart);
- // Get data from the source table as a SqlDataReader.
- SqlCommand commandSourceData = new SqlCommand(
- "SELECT ProductID, Name, " +
- "ProductNumber " +
- "FROM Production.Product;", sourceConnection);
- SqlDataReader reader =
- commandSourceData.ExecuteReader();
- // Open the destination connection. In the real world you would
- // not use SqlBulkCopy to move data from one table to the other
- // in the same database. This is for demonstration purposes only.
- using (SqlConnection destinationConnection =
- new SqlConnection(connectionString))
- {
- destinationConnection.Open();
- // Set up the bulk copy object.
- // Note that the column positions in the source
- // data reader match the column positions in
- // the destination table so there is no need to
- // map columns.
- using (SqlBulkCopy bulkCopy =
- new SqlBulkCopy(destinationConnection))
- {
- bulkCopy.DestinationTableName =
- "dbo.BulkCopyDemoMatchingColumns";
- try
- {
- // Write from the source to the destination.
- bulkCopy.WriteToServer(reader);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- // Close the SqlDataReader. The SqlBulkCopy
- // object is automatically closed at the end
- // of the using block.
- reader.Close();
- }
- }
- // Perform a final count on the destination
- // table to see how many rows were added.
- long countEnd = System.Convert.ToInt32(
- commandRowCount.ExecuteScalar());
- Console.WriteLine("Ending row count = {0}", countEnd);
- Console.WriteLine("{0} rows were added.", countEnd - countStart);
- Console.WriteLine("Press Enter to finish.");
- Console.ReadLine();
- }
- }
- }
- private static string GetConnectionString()
- // To avoid storing the sourceConnection string in your code,
- // you can retrieve it from a configuration file.
- {
- return "Data Source=(local); " +
- " Integrated Security=true;" +
- "Initial Catalog=AdventureWorks;";
- }
- }
SqlBulkCopy 快速插入数据的更多相关文章
- C#使用SqlTransaction事务回滚与SqlBulkCopy批量插入数据
C#中批量处理数据,有时候因为一条记录导致整个批量处理失败.这时候肯能会导致数据不全等问题,这时候我们可以使用SqlTransaction来进行事务回滚,即是要么全部成功要么全部不成功.如下代码 // ...
- SqlBulkCopy批量插入数据 显示 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 smalldatetime。错误
因为需要大量插入数据,linq ef无法达到速度的要求,因此把模型转换成SQL ,使用SqlBulkCopy快速插入.但是去提示 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 s ...
- sql 中的Bulk和C# 中的SqlBulkCopy批量插入数据 ( 回顾 and 粗谈 )
通常,我们会对于一个文本文件数据导入到数据库中,不多说,上代码. 首先,表结构如下. 其次,在我当前D盘中有个文本文件名为2.txt的文件. 在数据库中,可以这样通过一句代码插入. Bulk in ...
- SqlBulkCopy批量插入数据时,不执行触发器和约束的解决方法
原文:SqlBulkCopy批量插入数据时,不执行触发器和约束的解决方法 在new SqlBulkCopy对象的时候,设置一下SqlBulkCopyOptions选项即可,按位或运算 SqlBulkC ...
- C#中的SqlBulkCopy批量插入数据
在C#中,我们可以使用sqlBulkCopy去批量插入数据,其他批量插入方法不在讨论. 1 /// <summary> 2 /// SqlBulkCopy批量插入数据 3 /// < ...
- 使用SqlBulkCopy批量插入数据
static void Main(string[] args) { //定义与目标表结构相同的DataTable DataTable dataTable = new DataTable(); data ...
- 用SqlBulkCopy批量插入数据到SqlServer数据库表中
首先创建一个数据库连接类:SQLHelper using System; using System.Collections.Generic; using System.Linq; using Syst ...
- Presto向分区表快速插入数据时出现'target directory already exists'的原因
因为项目使用Presto作为ETL使用,需要将关系库中的数据导入到Hive中.目前关系库中的数据每天导入一次,在Hive中以天为间隔创建新的分区.思路是正确的,但是在使用的过程中,发现将少量关系库中的 ...
- c# sqlbulkcopy批量插入数据
dt信息中包含数据和表名 public static void SqlBulkInsert(DataTable dt, string connStr) { try { using (var conn ...
随机推荐
- (C/C++学习笔记) 十一. 数组
十一. 数组 ● 基本概念 数组:数组是一组在内存中依次连续存放的(数组所有元素在内存中的地址是连续的).具有同一类型的数据变量所组成的集合体.其中的每个变量称为数组元素,它们属于同一种数据类型,数组 ...
- 使用maven下载源码和doc(转)
原文链接: http://blog.csdn.net/sxdtzhaoxinguo/article/details/46518295 http://blog.csdn.net/chengxusheji ...
- ide fix pack for delph 10.2.3发布了
http://andy.jgknet.de/blog/ide-tools/ide-fix-pack/ IDE Fix Pack是RAD Studio IDE,Win32 / Win64 / Andoi ...
- Vue CLI 3 配置兼容IE10
最近做了一个基于Vue的项目,需要兼容IE浏览器,目前实现了打包后可以在IE10以上运行,但是还不支持在运行时兼容IE10及以上. 安装依赖 yarn add --dev @babel/polyfil ...
- Linux下XordDos木马的清除
朋友的阿里云服务器一早上报木马入侵,找我处理,登陆阿里云查看警告信息“恶意进程(云查杀)-XorDDoS木马”, 本文也可以作为服务器处理木马排查的步骤的参考文章 排查原则: 1.一般的木马都有多个守 ...
- 判断颜色信息-RGB2HSV(opencv)
前言 项目车号识别过程中,车体有三种颜色黑车黑底白字.红车红底白字.绿车黄底绿字,可以通过判断车体的颜色信息,从而判断二值化是否需要反转,主要是基于rgb2hsv函数进行不同颜色的阈值判断. matl ...
- 20155328 2016-2017-2 《Java程序设计》第5周学习总结
教材学习内容总结 程序设计本身的错误,建议使用Exception或其子类实例来表现. Java中所有错误都会被打包成对象. 如果父类异常对象在子类异常对象前被捕捉,则catch子类异常对象的区块将永远 ...
- 利用Fierce2查询子域名
http://pnig0s1992.blog.51cto.com/393390/368428 安装方法引用Mickey的: 1.Mickey@pentestbox:/pentest/enumerati ...
- test20180922 古代龙人的谜题
题意 问题描述 Mark Douglas是一名调查员.他接受了「调查古代龙人」的任务.经过千辛万苦,Mark终于找到了一位古代龙人.Mark找到他时,他正在摆弄一些秘药,其中一些药丸由于是从很久以前流 ...
- BT601. BT709色彩空间
参考:http://blog.csdn.net/mao0514/article/details/16958873