博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SqlBulkCopy 快速插入数据
阅读量:5115 次
发布时间:2019-06-13

本文共 3614 字,大约阅读时间需要 12 分钟。

[转]本文来自
此代码用于演示仅使用 
SqlBulkCopy 的语法。如果源表和目标表都在同一个 SQL Server 实例中,则使用 Transact-SQL 
INSERT … SELECT 语句复制数据会更方便快捷。
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;";    }}

  

转载于:https://www.cnblogs.com/z5337/p/4126741.html

你可能感兴趣的文章
92. Reverse Linked List II
查看>>
vs2008 MFC类继承结构
查看>>
windows版mysql添加远程访问
查看>>
hdoj3652 B-number
查看>>
16-基础-过滤器-传参数和串联使用
查看>>
两种排序方法(直接判断)
查看>>
学习进度第十四周
查看>>
这20个正则表达式,让你少写1,000行代码
查看>>
文本两端对齐
查看>>
【BZOJ3992】【SDOI2015】序列统计
查看>>
Oracle数据库执行Sql脚本
查看>>
数据结构上机实验:单链表操作
查看>>
UEFI+GPT安装Win10和RHEL6.5双系统
查看>>
视觉SLAM算法框架解析(1) PTAM
查看>>
The Towers of Hanoi Revisited---(多柱汉诺塔)
查看>>
listen()函数中的SOMAXCONN含义
查看>>
Windows下的Qt Creator的安装
查看>>
hibernate文档
查看>>
区分汉字与英文
查看>>
div常用效果方法-transform
查看>>