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 @ 2011-08-02 11:08  贝加  阅读(576)  评论(0编辑  收藏  举报