此代码用于演示仅使用 SqlBulkCopy 的语法。如果源表和目标表都在同一个 SQL Server 实例中,则使用 Transact-SQL INSERT … SELECT 语句复制数据会更方便快捷。
  1. using System.Data.SqlClient;
  2.  
  3. class Program
  4. {
  5. static void Main()
  6. {
  7. string connectionString = GetConnectionString();
  8. // Open a sourceConnection to the AdventureWorks database.
  9. using (SqlConnection sourceConnection =
  10. new SqlConnection(connectionString))
  11. {
  12. sourceConnection.Open();
  13.  
  14. // Perform an initial count on the destination table.
  15. SqlCommand commandRowCount = new SqlCommand(
  16. "SELECT COUNT(*) FROM " +
  17. "dbo.BulkCopyDemoMatchingColumns;",
  18. sourceConnection);
  19. long countStart = System.Convert.ToInt32(
  20. commandRowCount.ExecuteScalar());
  21. Console.WriteLine("Starting row count = {0}", countStart);
  22.  
  23. // Get data from the source table as a SqlDataReader.
  24. SqlCommand commandSourceData = new SqlCommand(
  25. "SELECT ProductID, Name, " +
  26. "ProductNumber " +
  27. "FROM Production.Product;", sourceConnection);
  28. SqlDataReader reader =
  29. commandSourceData.ExecuteReader();
  30.  
  31. // Open the destination connection. In the real world you would
  32. // not use SqlBulkCopy to move data from one table to the other
  33. // in the same database. This is for demonstration purposes only.
  34. using (SqlConnection destinationConnection =
  35. new SqlConnection(connectionString))
  36. {
  37. destinationConnection.Open();
  38.  
  39. // Set up the bulk copy object.
  40. // Note that the column positions in the source
  41. // data reader match the column positions in
  42. // the destination table so there is no need to
  43. // map columns.
  44. using (SqlBulkCopy bulkCopy =
  45. new SqlBulkCopy(destinationConnection))
  46. {
  47. bulkCopy.DestinationTableName =
  48. "dbo.BulkCopyDemoMatchingColumns";
  49.  
  50. try
  51. {
  52. // Write from the source to the destination.
  53. bulkCopy.WriteToServer(reader);
  54. }
  55. catch (Exception ex)
  56. {
  57. Console.WriteLine(ex.Message);
  58. }
  59. finally
  60. {
  61. // Close the SqlDataReader. The SqlBulkCopy
  62. // object is automatically closed at the end
  63. // of the using block.
  64. reader.Close();
  65. }
  66. }
  67.  
  68. // Perform a final count on the destination
  69. // table to see how many rows were added.
  70. long countEnd = System.Convert.ToInt32(
  71. commandRowCount.ExecuteScalar());
  72. Console.WriteLine("Ending row count = {0}", countEnd);
  73. Console.WriteLine("{0} rows were added.", countEnd - countStart);
  74. Console.WriteLine("Press Enter to finish.");
  75. Console.ReadLine();
  76. }
  77. }
  78. }
  79.  
  80. private static string GetConnectionString()
  81. // To avoid storing the sourceConnection string in your code,
  82. // you can retrieve it from a configuration file.
  83. {
  84. return "Data Source=(local); " +
  85. " Integrated Security=true;" +
  86. "Initial Catalog=AdventureWorks;";
  87. }
  88. }

  

SqlBulkCopy 快速插入数据的更多相关文章

  1. C#使用SqlTransaction事务回滚与SqlBulkCopy批量插入数据

    C#中批量处理数据,有时候因为一条记录导致整个批量处理失败.这时候肯能会导致数据不全等问题,这时候我们可以使用SqlTransaction来进行事务回滚,即是要么全部成功要么全部不成功.如下代码 // ...

  2. SqlBulkCopy批量插入数据 显示 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 smalldatetime。错误

    因为需要大量插入数据,linq ef无法达到速度的要求,因此把模型转换成SQL ,使用SqlBulkCopy快速插入.但是去提示 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 s ...

  3. sql 中的Bulk和C# 中的SqlBulkCopy批量插入数据 ( 回顾 and 粗谈 )

    通常,我们会对于一个文本文件数据导入到数据库中,不多说,上代码. 首先,表结构如下.   其次,在我当前D盘中有个文本文件名为2.txt的文件. 在数据库中,可以这样通过一句代码插入. Bulk in ...

  4. SqlBulkCopy批量插入数据时,不执行触发器和约束的解决方法

    原文:SqlBulkCopy批量插入数据时,不执行触发器和约束的解决方法 在new SqlBulkCopy对象的时候,设置一下SqlBulkCopyOptions选项即可,按位或运算 SqlBulkC ...

  5. C#中的SqlBulkCopy批量插入数据

    在C#中,我们可以使用sqlBulkCopy去批量插入数据,其他批量插入方法不在讨论. 1 /// <summary> 2 /// SqlBulkCopy批量插入数据 3 /// < ...

  6. 使用SqlBulkCopy批量插入数据

    static void Main(string[] args) { //定义与目标表结构相同的DataTable DataTable dataTable = new DataTable(); data ...

  7. 用SqlBulkCopy批量插入数据到SqlServer数据库表中

    首先创建一个数据库连接类:SQLHelper using System; using System.Collections.Generic; using System.Linq; using Syst ...

  8. Presto向分区表快速插入数据时出现'target directory already exists'的原因

    因为项目使用Presto作为ETL使用,需要将关系库中的数据导入到Hive中.目前关系库中的数据每天导入一次,在Hive中以天为间隔创建新的分区.思路是正确的,但是在使用的过程中,发现将少量关系库中的 ...

  9. c# sqlbulkcopy批量插入数据

    dt信息中包含数据和表名 public static void SqlBulkInsert(DataTable dt, string connStr) { try { using (var conn ...

随机推荐

  1. (C/C++学习笔记) 十一. 数组

    十一. 数组 ● 基本概念 数组:数组是一组在内存中依次连续存放的(数组所有元素在内存中的地址是连续的).具有同一类型的数据变量所组成的集合体.其中的每个变量称为数组元素,它们属于同一种数据类型,数组 ...

  2. 使用maven下载源码和doc(转)

    原文链接: http://blog.csdn.net/sxdtzhaoxinguo/article/details/46518295 http://blog.csdn.net/chengxusheji ...

  3. 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 ...

  4. Vue CLI 3 配置兼容IE10

    最近做了一个基于Vue的项目,需要兼容IE浏览器,目前实现了打包后可以在IE10以上运行,但是还不支持在运行时兼容IE10及以上. 安装依赖 yarn add --dev @babel/polyfil ...

  5. Linux下XordDos木马的清除

    朋友的阿里云服务器一早上报木马入侵,找我处理,登陆阿里云查看警告信息“恶意进程(云查杀)-XorDDoS木马”, 本文也可以作为服务器处理木马排查的步骤的参考文章 排查原则: 1.一般的木马都有多个守 ...

  6. 判断颜色信息-RGB2HSV(opencv)

    前言 项目车号识别过程中,车体有三种颜色黑车黑底白字.红车红底白字.绿车黄底绿字,可以通过判断车体的颜色信息,从而判断二值化是否需要反转,主要是基于rgb2hsv函数进行不同颜色的阈值判断. matl ...

  7. 20155328 2016-2017-2 《Java程序设计》第5周学习总结

    教材学习内容总结 程序设计本身的错误,建议使用Exception或其子类实例来表现. Java中所有错误都会被打包成对象. 如果父类异常对象在子类异常对象前被捕捉,则catch子类异常对象的区块将永远 ...

  8. 利用Fierce2查询子域名

    http://pnig0s1992.blog.51cto.com/393390/368428 安装方法引用Mickey的: 1.Mickey@pentestbox:/pentest/enumerati ...

  9. test20180922 古代龙人的谜题

    题意 问题描述 Mark Douglas是一名调查员.他接受了「调查古代龙人」的任务.经过千辛万苦,Mark终于找到了一位古代龙人.Mark找到他时,他正在摆弄一些秘药,其中一些药丸由于是从很久以前流 ...

  10. BT601. BT709色彩空间

    参考:http://blog.csdn.net/mao0514/article/details/16958873