SqlBulkCopy 类

本文来自CSDN:http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlbulkcopy(VS.80).aspx

 

MS SQL Server 提供一个称为 bcp 的流行的命令提示符实用工具,用于将数据从一个表移动到另一个表(表可以在不同服务器上)。

SqlBulkCopy 类允许编写提供类似功能的托管代码解决方案。相比其他将数据加载到 SQL Server 表的方法(例如 INSERT 语句), SqlBulkCopy 提供明显的性能优势。(经测试数据量越大性能越明显,至少是批量insert语句的25倍以上。)

使用 SqlBulkCopy 类只能向 SQL Server 表写入数据。但是数据源不限于 SQL Server,可以使用任何数据源,只要数据可加载到 DataTable 实例或可使用 IDataReader 实例读取数据。

SqlBulkcopy对象主要作为作为了提高批量插入的效率,所以在默认情况下批量数据的插入并不会引发insert触发器的触发。这可以通过使用SqlBulkCopy (String, SqlBulkCopyOptions) 指定enum SqlBulkCopyOptionsFireTriggers解决

注意:使用SqlBulkcopy时在inserted表中是有多条记录的,且一次调用SqlBulkcopy只会有一次触发器的运行。

使用示例:

using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "server=.;database=AdventureWorks;uid=sa;pwd=sa";
        // 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();
            }
        }
    }

}

posted @   贝加  阅读(577)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
阅读排行:
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 提示词工程师自白:我如何用一个技巧解放自己的生产力
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· 如何不购买域名在云服务器上搭建HTTPS服务
点击右上角即可分享
微信分享提示