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如果提供 Unspecified 的 , ArgumentException 將會擲回 。

建構函式

SqlBulkCopyColumnOrderHint(String, SortOrder)

為指定的目的地資料行建立新的資料行順序提示。

屬性

Column

要提供提示之目的地資料表中的目的地資料行名稱。

SortOrder

目的地資料表中目的地資料行的排序次序。

適用於