SqlBulkCopyColumnMapping 類別

定義

定義 SqlBulkCopy 執行個體 (instance) 之資料來源中資料行,與執行個體之目的資料表中資料行間的對應。

public ref class SqlBulkCopyColumnMapping sealed
public sealed class SqlBulkCopyColumnMapping
type SqlBulkCopyColumnMapping = class
Public NotInheritable Class SqlBulkCopyColumnMapping
繼承
SqlBulkCopyColumnMapping

範例

下列範例會從 AdventureWorks 範例資料庫中的來源資料表,將資料大量複製到相同資料庫中的目的地資料表。 雖然目的地中的資料行數目符合來源中的資料行數目,而且每個目的地資料行位於與其對應來源資料行相同的序數位位置,但資料行名稱不相符。 SqlBulkCopyColumnMapping 物件可用來建立大量複製的資料行對應。

重要

除非您已建立工作資料表,如 大量複製範例安裝程式中所述,否則不會執行此範例。 這個程式碼僅是為了示範使用 SqlBulkCopy 的語法而提供。 如果來源和目的地資料表位於相同的SQL Server實例中,使用 Transact-SQL INSERT … SELECT 語句來複製資料會比較簡單且更快速。

using System.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.BulkCopyDemoDifferentColumns;",
                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.BulkCopyDemoDifferentColumns";

                // Set up the column mappings by name.
                SqlBulkCopyColumnMapping mapID =
                    new SqlBulkCopyColumnMapping("ProductID", "ProdID");
                bulkCopy.ColumnMappings.Add(mapID);

                SqlBulkCopyColumnMapping mapName =
                    new SqlBulkCopyColumnMapping("Name", "ProdName");
                bulkCopy.ColumnMappings.Add(mapName);

                SqlBulkCopyColumnMapping mapMumber =
                    new SqlBulkCopyColumnMapping("ProductNumber", "ProdNum");
                bulkCopy.ColumnMappings.Add(mapMumber);

                // 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;";
    }
}
Imports System.Data.SqlClient

Module Module1
    Sub Main()
        Dim connectionString As String = GetConnectionString()

        ' Open a connection to the AdventureWorks database.
        Using sourceConnection As SqlConnection = _
           New SqlConnection(connectionString)
            sourceConnection.Open()

            ' Perform an initial count on the destination table.
            Dim commandRowCount As New SqlCommand( _
            "SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns;", _
                sourceConnection)
            Dim countStart As Long = _
               System.Convert.ToInt32(commandRowCount.ExecuteScalar())
            Console.WriteLine("Starting row count = {0}", countStart)

            ' Get data from the source table as a SqlDataReader.
            Dim commandSourceData As SqlCommand = New SqlCommand( _
               "SELECT ProductID, Name, ProductNumber " & _
               "FROM Production.Product;", sourceConnection)
            Dim reader As SqlDataReader = commandSourceData.ExecuteReader

            ' Set up the bulk copy object.
            Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(connectionString)
                bulkCopy.DestinationTableName = _
                "dbo.BulkCopyDemoDifferentColumns"

                ' Set up the column mappings by name.
                Dim mapID As New _
                  SqlBulkCopyColumnMapping("ProductID", "ProdID")
                bulkCopy.ColumnMappings.Add(mapID)

                Dim mapName As New _
                 SqlBulkCopyColumnMapping("Name", "ProdName")
                bulkCopy.ColumnMappings.Add(mapName)

                Dim mapMumber As New _
                 SqlBulkCopyColumnMapping("ProductNumber", "ProdNum")
                bulkCopy.ColumnMappings.Add(mapMumber)

                ' Write from the source to the destination.
                Try
                    bulkCopy.WriteToServer(reader)

                Catch ex As Exception
                    Console.WriteLine(ex.Message)

                Finally
                    ' Close the SqlDataReader. The SqlBulkCopy
                    ' object is automatically closed at the end
                    ' of the Using block.
                    reader.Close()
                End Try
            End Using

            ' Perform a final count on the destination table
            ' to see how many rows were added.
            Dim countEnd As Long = _
                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()
        End Using
    End Sub

    Private Function GetConnectionString() As String
        ' 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;"
    End Function
End Module

備註

資料行對應會定義資料來源與目標資料表之間的對應。

如果未定義對應,也就是 ColumnMappings 集合是空的, 資料行會根據序數位置隱含對應。 若要執行這項操作,則來源和目標結構描述必須相符。 如果沒有, InvalidOperationException 將會擲回 。

如果集合不是空的 ColumnMappings ,則不需要指定資料來源中的每個資料行。 不會由集合對應的專案會被忽略。

您可以依名稱或序數找到來源和目標資料行。 您也可以在相同的對應集合中混用依名稱與序數資料行參考。

建構函式

SqlBulkCopyColumnMapping()

初始化新 SqlBulkCopyColumnMapping 物件的無參數建構函式。

SqlBulkCopyColumnMapping(Int32, Int32)

建立新的資料行對應,使用資料行序數參考來源和目的資料行。

SqlBulkCopyColumnMapping(Int32, String)

建立新的資料行對應,使用資料行序數參考來源資料行和目標資料行的資料行名稱。

SqlBulkCopyColumnMapping(String, Int32)

建立新的資料行對應,使用資料行名稱參考來源資料行和目標資料行的資料行序數。

SqlBulkCopyColumnMapping(String, String)

建立新的資料行對應,使用資料行名稱參考來源和目的資料行。

屬性

DestinationColumn

目的資料庫資料表中正在對應的資料行名稱。

DestinationOrdinal

目的資料表中目的資料行的序數值。

SourceColumn

資料來源中正在對應的資料行名稱。

SourceOrdinal

資料來源內來源資料行的序數位置。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱