다음을 통해 공유


SqlBulkCopyColumnOrderHint.SortOrder 속성

정의

대상 테이블의 대상 열 정렬 순서입니다.

public:
 property Microsoft::Data::SqlClient::SortOrder SortOrder { Microsoft::Data::SqlClient::SortOrder get(); void set(Microsoft::Data::SqlClient::SortOrder value); };
public Microsoft.Data.SqlClient.SortOrder SortOrder { get; set; }
member this.SortOrder : Microsoft.Data.SqlClient.SortOrder with get, set
Public Property SortOrder As SortOrder

속성 값

속성의 SortOrder 값입니다 SortOrder .

예외

열 순서 힌트에 대해 정렬 순서를 지정할 수 없습니다.

예제

다음 예제에서는 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);
                hintNumber.SortOrder = SortOrder.Descending;
                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;";
    }
}

설명

ArgumentException 지정되지 않은 의 가 SortOrder 지정되지 않은 경우 이 throw됩니다.

적용 대상