SqlBulkCopyColumnMappingCollection Class
Collapse the table of content
Expand the table of content

SqlBulkCopyColumnMappingCollection Class


Collection of SqlBulkCopyColumnMapping objects that inherits from CollectionBase.

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


public sealed class SqlBulkCopyColumnMappingCollection : CollectionBase


Gets or sets the number of elements that the CollectionBase can contain.(Inherited from CollectionBase.)


Gets the number of elements contained in the CollectionBase instance. This property cannot be overridden.(Inherited from CollectionBase.)


Gets the SqlBulkCopyColumnMapping object at the specified index.

System_CAPS_pubmethodAdd(Int32, Int32)

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

System_CAPS_pubmethodAdd(Int32, String)

Creates a new SqlBulkCopyColumnMapping and adds it to the collection, using an ordinal for the source column and a string for the destination column.


Adds the specified mapping to the SqlBulkCopyColumnMappingCollection.

System_CAPS_pubmethodAdd(String, Int32)

Creates a new SqlBulkCopyColumnMapping and adds it to the collection, using a column name to describe the source column and an ordinal to specify the destination column.

System_CAPS_pubmethodAdd(String, String)

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


Clears the contents of the collection.


Gets a value indicating whether a specified SqlBulkCopyColumnMapping object exists in the collection.

System_CAPS_pubmethodCopyTo(SqlBulkCopyColumnMapping[], Int32)

Copies the elements of the SqlBulkCopyColumnMappingCollection to an array of SqlBulkCopyColumnMapping items, starting at a particular index.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Returns an enumerator that iterates through the CollectionBase instance.(Inherited from CollectionBase.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Gets the index of the specified SqlBulkCopyColumnMapping object.

System_CAPS_pubmethodInsert(Int32, SqlBulkCopyColumnMapping)

Insert a new SqlBulkCopyColumnMapping at the index specified.


Removes the specified SqlBulkCopyColumnMapping element from the SqlBulkCopyColumnMappingCollection.


Removes the mapping at the specified index from the collection.


Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

Copies the entire CollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.(Inherited from CollectionBase.)


Adds an object to the end of the CollectionBase.(Inherited from CollectionBase.)


Determines whether the CollectionBase contains a specific element.(Inherited from CollectionBase.)


Searches for the specified Object and returns the zero-based index of the first occurrence within the entire CollectionBase.(Inherited from CollectionBase.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Insert(Int32, Object)

Inserts an element into the CollectionBase at the specified index.(Inherited from CollectionBase.)


Removes the first occurrence of a specific object from the CollectionBase.(Inherited from CollectionBase.)


Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)


Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)


Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)


Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

Column mappings define the mapping between data source and the target table.

If mappings are not defined—that is, the ColumnMappings collection is empty—the columns are mapped implicitly based on ordinal position. For this to work, source and target schemas must match. If they do not, an InvalidOperationException is thrown.

If the ColumnMappings collection is not empty, not every column present in the data source has to be specified. Those not mapped by the collection are ignored.

You can refer to source and target columns by either name or ordinal. You can mix by-name and by-ordinal column references in the same mappings collection.

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 are added to the SqlBulkCopyColumnMappingCollection for the SqlBulkCopy object to create a column map for the bulk copy.


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("ProductID", "ProdID");
                bulkCopy.ColumnMappings.Add("Name", "ProdName");
                bulkCopy.ColumnMappings.Add("ProductNumber", "ProdNum");

                // 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
Available since 2.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft