共用方式為


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

適用於