使用英语阅读

通过


SqlBulkCopy.BatchSize 属性

定义

每批中的行数。 在每批结束时,会将批中的行发送到服务器。

public int BatchSize { get; set; }

属性值

BatchSize 属性的整数值;如果未设置任何值,则为零。

示例

以下控制台应用程序演示如何以 50 行的批处理批量加载数据。 有关说明如何使用 BatchSize 事务的示例,请参阅 事务和大容量复制操作

在此示例中,源数据首先从SQL Server表读取到SqlDataReader实例。 源数据不必位于 SQL Server;可以使用可读取到 或加载到 IDataReader 的任何DataTable数据源。

重要

除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。 提供此代码是为了演示仅使用 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();

            // Create the SqlBulkCopy object using a connection string.
            // In the real world you would not use SqlBulkCopy to move
            // data from one table to the other in the same database.
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
            {
                bulkCopy.DestinationTableName =
                    "dbo.BulkCopyDemoMatchingColumns";

                // Set the BatchSize.
                bulkCopy.BatchSize = 50;

                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;";
    }
}

注解

当处理完 BatchSize 行或没有更多行要发送到目标数据源时,即表示批已完成。

零 (默认) 表示每个 WriteToServer 操作都是单个批处理。

SqlBulkCopy如果声明实例时没有UseInternalTransaction有效选项,则行将一次发送到服务器BatchSize行,但不执行与事务相关的操作。 如果 UseInternalTransaction 有效,则每批行作为单独的事务插入。

BatchSize可以随时设置 属性。 如果批量复制已在进行中,则根据以前的批大小调整当前批的大小。 后续批处理使用新大小。 BatchSize如果 最初为零并在操作正在进行时WriteToServer更改,则该操作将作为单个批处理加载数据。 同一SqlBulkCopy实例上的任何后续WriteToServer操作都使用新的 BatchSize

适用于

产品 版本
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

另请参阅