Share via


SqlBulkCopyColumnOrderHintCollection.Add 方法

定义

重载

Add(SqlBulkCopyColumnOrderHint)

将指定的顺序提示添加到 。SqlBulkCopyColumnOrderHintCollection

Add(String, SortOrder)

创建一个新的 SqlBulkCopyColumnOrderHint,并将其添加到集合中。

Add(SqlBulkCopyColumnOrderHint)

将指定的顺序提示添加到 。SqlBulkCopyColumnOrderHintCollection

public:
 Microsoft::Data::SqlClient::SqlBulkCopyColumnOrderHint ^ Add(Microsoft::Data::SqlClient::SqlBulkCopyColumnOrderHint ^ columnOrderHint);
public Microsoft.Data.SqlClient.SqlBulkCopyColumnOrderHint Add (Microsoft.Data.SqlClient.SqlBulkCopyColumnOrderHint columnOrderHint);
member this.Add : Microsoft.Data.SqlClient.SqlBulkCopyColumnOrderHint -> Microsoft.Data.SqlClient.SqlBulkCopyColumnOrderHint
Public Function Add (columnOrderHint As SqlBulkCopyColumnOrderHint) As SqlBulkCopyColumnOrderHint

参数

columnOrderHint
SqlBulkCopyColumnOrderHint

描述 SqlBulkCopyColumnOrderHint 要添加到集合中的顺序提示的 对象。

返回

SqlBulkCopyColumnOrderHint 对象。

例外

该值为 Null。

示例

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

重要

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

// <Snippet1>
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";

                // Specify the sort order for the ProductNumber column in 
                // the destination table.
                // 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;";
    }
}
// </Snippet1>

适用于

Add(String, SortOrder)

创建一个新的 SqlBulkCopyColumnOrderHint,并将其添加到集合中。

public:
 Microsoft::Data::SqlClient::SqlBulkCopyColumnOrderHint ^ Add(System::String ^ column, Microsoft::Data::SqlClient::SortOrder sortOrder);
public Microsoft.Data.SqlClient.SqlBulkCopyColumnOrderHint Add (string column, Microsoft.Data.SqlClient.SortOrder sortOrder);
member this.Add : string * Microsoft.Data.SqlClient.SortOrder -> Microsoft.Data.SqlClient.SqlBulkCopyColumnOrderHint
Public Function Add (column As String, sortOrder As SortOrder) As SqlBulkCopyColumnOrderHint

参数

column
String

目标表中目标列的名称。

sortOrder
SortOrder

相应目标列的排序顺序。

返回

列列顺序提示。

示例

以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 通过提供目标列名称及其排序顺序,将 SqlBulkCopyColumnOrderHint 对象添加到 ColumnOrderHints

重要

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

                // Specify the sort order for the ProductNumber column in 
                // the destination table.
                bulkCopy.ColumnOrderHints.Add("ProductNumber", SortOrder.Ascending);

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

适用于