SqlBulkCopyColumnOrderHint 类

定义

根据表上的聚集索引,定义实例的目标表中列 SqlBulkCopy 的排序顺序。

public ref class SqlBulkCopyColumnOrderHint sealed
public sealed class SqlBulkCopyColumnOrderHint
type SqlBulkCopyColumnOrderHint = class
Public NotInheritable Class SqlBulkCopyColumnOrderHint
继承
SqlBulkCopyColumnOrderHint

示例

以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 SqlBulkCopyColumnOrderHint 对象用于定义 ProductNumber 目标列的排序顺序。

重要

除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。 提供此代码是为了演示仅使用 SqlBulkCopy 时的语法。 如果源表和目标表位于同一SQL Server实例中,则使用 Transact-SQL INSERT … SELECT 语句复制数据会更轻松、更快。

using System;
using System.Data;
using Microsoft.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();

            // Set up the bulk copy object.
            using (SqlBulkCopy bulkCopy =
                       new SqlBulkCopy(connectionString))
            {
                bulkCopy.DestinationTableName =
                    "dbo.BulkCopyDemoMatchingColumns";

                // Setup an order hint for the ProductNumber column.
                SqlBulkCopyColumnOrderHint hintNumber =
                    new SqlBulkCopyColumnOrderHint("ProductNumber", SortOrder.Ascending);
                bulkCopy.ColumnOrderHints.Add(hintNumber);

                // Write from the source to the destination.
                try
                {
                    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;";
    }
}

注解

列顺序提示定义目标表中列的排序顺序。

如果导入的数据根据表上的聚集索引(如果有)进行排序,则 SqlBulkCopy 的性能会得到提高。 如果数据的排序顺序不同于聚集索引键的顺序,或者表中没有聚集索引,则忽略顺序提示。

可以为目标表中任意数量的列指定顺序提示。 默认情况下,如果未提供提示,则大容量插入操作假定数据是无序的。

提供的列名必须是目标表中有效的列名。 提示的指定顺序是任意的。 单个列名不能指定多次。

ColumnMappings如果集合不为空,则只能为已映射的有效目标列提供订单提示。

SortOrder如果提供了 “未指定” ,ArgumentException则会引发 。

构造函数

SqlBulkCopyColumnOrderHint(String, SortOrder)

为指定的目标列创建新的列顺序提示。

属性

Column

要为其提供提示的目标表中的目标列的名称。

SortOrder

目标表中目标列的排序顺序。

适用于