Export (0) Print
Expand All

SqlBulkCopyColumnMappingCollection.Add Method (Int32, Int32)

Creates a new SqlBulkCopyColumnMapping and adds it to the collection, using ordinals to specify both source and destination columns.

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

public SqlBulkCopyColumnMapping Add(
	int sourceColumnIndex,
	int destinationColumnIndex


Type: System.Int32

The ordinal position of the source column within the data source.

Type: System.Int32

The ordinal position of the destination column within the destination table.

Return Value

Type: System.Data.SqlClient.SqlBulkCopyColumnMapping
A column mapping.

Mappings in a collection must be uniform: either all integer/integer pairs, all string/string pairs, all integer/string pairs, or all string/integer pairs. If you try to add a mapping that is different from others already in the collection, an InvalidOperationException is thrown.

The following example bulk copies data from a source table in the AdventureWorks sample database to a destination table in the same database. Although the number of columns in the destination matches the number of columns in the source, the column names and ordinal positions do not match. SqlBulkCopyColumnMapping objects are used to create a column map for the bulk copy using the ordinal position of the source and destination columns.

Important noteImportant

This sample will not run unless you have created the work tables as described in Bulk Copy Example Setup. This code is provided to demonstrate the syntax for using SqlBulkCopy only. If the source and destination tables are in the same SQL Server instance, it is easier and faster to use a Transact-SQL INSERT … SELECT statement to copy the data.

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))

            // Perform an initial count on the destination table.
            SqlCommand commandRowCount = new SqlCommand(
                "SELECT COUNT(*) FROM " +
            long countStart = System.Convert.ToInt32(
            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 =

            // Set up the bulk copy object.  
            using (SqlBulkCopy bulkCopy =
                       new SqlBulkCopy(connectionString))
                bulkCopy.DestinationTableName =

                // The column order in the source doesn't match the order  
                // in the destination, so ColumnMappings must be defined.
                bulkCopy.ColumnMappings.Add(0, 0);
                bulkCopy.ColumnMappings.Add(1, 2);
                bulkCopy.ColumnMappings.Add(2, 1);

                // Write from the source to the destination. 
                catch (Exception ex)
                    // Close the SqlDataReader. The SqlBulkCopy 
                    // object is automatically closed at the end 
                    // of the using block.

            // Perform a final count on the destination  
            // table to see how many rows were added. 
            long countEnd = System.Convert.ToInt32(
            Console.WriteLine("Ending row count = {0}", countEnd);
            Console.WriteLine("{0} rows were added.", countEnd - countStart);
            Console.WriteLine("Press Enter to finish.");

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft