Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SqlBulkCopyColumnMappingCollection.Remove Method

Removes the specified SqlBulkCopyColumnMapping element from the SqlBulkCopyColumnMappingCollection.

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

public void Remove(
	SqlBulkCopyColumnMapping value


Type: System.Data.SqlClient.SqlBulkCopyColumnMapping

SqlBulkCopyColumnMapping object to be removed from the collection.

The Remove method is most commonly used when you use a single SqlBulkCopy instance to process more than one bulk copy operation. If you create column mappings for one bulk copy operation, you must remove mappings that no longer apply after the WriteToServer method is called and before defining mapping for the next bulk copy. You can clear the entire collection by using the Clear method, or remove mappings individually using the Remove method or the RemoveAt method.

Performing several bulk copies using the same SqlBulkCopy instance will usually be more efficient from a performance point of view than using a separate SqlBulkCopy for each operation.

The following example performs two bulk copy operations. The first operation copies sales order header information, and the second copies sales order details. Although not strictly necessary in this example (because the ordinal positions of the source and destination columns do match), the example defines column mappings for each bulk copy operation. Both bulk copies include a mapping for the SalesOrderID, so rather than clearing the entire collection between bulk copy operations, the example removes all mappings except for the SalesOrderID mapping and then adds the appropriate mappings for the second bulk copy operation.

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 connection to the AdventureWorks database. 
        using (SqlConnection connection =
                   new SqlConnection(connectionString))

            // Empty the destination tables. 
            SqlCommand deleteHeader = new SqlCommand(
                "DELETE FROM dbo.BulkCopyDemoOrderHeader;",
            SqlCommand deleteDetail = new SqlCommand(
                "DELETE FROM dbo.BulkCopyDemoOrderDetail;",

            // Perform an initial count on the destination 
            //  table with matching columns. 
            SqlCommand countRowHeader = new SqlCommand(
                "SELECT COUNT(*) FROM dbo.BulkCopyDemoOrderHeader;",
            long countStartHeader = System.Convert.ToInt32(
                "Starting row count for Header table = {0}",

            // Perform an initial count on the destination 
            // table with different column positions. 
            SqlCommand countRowDetail = new SqlCommand(
                "SELECT COUNT(*) FROM dbo.BulkCopyDemoOrderDetail;",
            long countStartDetail = System.Convert.ToInt32(
                "Starting row count for Detail table = {0}",

            // Get data from the source table as a SqlDataReader. 
            // The Sales.SalesOrderHeader and Sales.SalesOrderDetail 
            // tables are quite large and could easily cause a timeout 
            // if all data from the tables is added to the destination.  
            // To keep the example simple and quick, a parameter is   
            // used to select only orders for a particular account  
            // as the source for the bulk insert. 
            SqlCommand headerData = new SqlCommand(
                "SELECT [SalesOrderID], [OrderDate], " +
                "[AccountNumber] FROM [Sales].[SalesOrderHeader] " +
                "WHERE [AccountNumber] = @accountNumber;",
            SqlParameter parameterAccount = new SqlParameter();
            parameterAccount.ParameterName = "@accountNumber";
            parameterAccount.SqlDbType = SqlDbType.NVarChar;
            parameterAccount.Direction = ParameterDirection.Input;
            parameterAccount.Value = "10-4020-000034";
            SqlDataReader readerHeader = headerData.ExecuteReader();

            // Get the Detail data in a separate connection. 
            using (SqlConnection connection2 = new SqlConnection(connectionString))
                SqlCommand sourceDetailData = new SqlCommand(
                    "SELECT [Sales].[SalesOrderDetail].[SalesOrderID], [SalesOrderDetailID], " +
                    "[OrderQty], [ProductID], [UnitPrice] FROM [Sales].[SalesOrderDetail] " +
                    "INNER JOIN [Sales].[SalesOrderHeader] ON [Sales].[SalesOrderDetail]." +
                    "[SalesOrderID] = [Sales].[SalesOrderHeader].[SalesOrderID] " +
                    "WHERE [AccountNumber] = @accountNumber;", connection2);

                SqlParameter accountDetail = new SqlParameter();
                accountDetail.ParameterName = "@accountNumber";
                accountDetail.SqlDbType = SqlDbType.NVarChar;
                accountDetail.Direction = ParameterDirection.Input;
                accountDetail.Value = "10-4020-000034";
                SqlDataReader readerDetail = sourceDetailData.ExecuteReader();

                // Create the SqlBulkCopy object.  
                using (SqlBulkCopy bulkCopy =
                           new SqlBulkCopy(connectionString))
                    bulkCopy.DestinationTableName =

                    // Guarantee that columns are mapped correctly by 
                    // defining the column mappings for the order.
                    bulkCopy.ColumnMappings.Add("SalesOrderID", "SalesOrderID");

                    // Define SqlBulkCopyColumnMapping objects so they 
                    // can be referenced later.
                    SqlBulkCopyColumnMapping columnMappingDate =
                        bulkCopy.ColumnMappings.Add("OrderDate", "OrderDate");
                    SqlBulkCopyColumnMapping columnMappingAccount =
                        bulkCopy.ColumnMappings.Add("AccountNumber", "AccountNumber");

                    // Write readerHeader to the destination. 
                    catch (Exception ex)

                    // Set up the order details destination. 
                    bulkCopy.DestinationTableName ="dbo.BulkCopyDemoOrderDetail";

                    // Rather than clearing mappings that are not necessary 
                    // for the next bulk copyo peration, the unneeded mappings 
                    //  are removed with the Remove method.

                    // Add order detail column mappings.
                    bulkCopy.ColumnMappings.Add("SalesOrderDetailID", "SalesOrderDetailID");
                    bulkCopy.ColumnMappings.Add("OrderQty", "OrderQty");
                    bulkCopy.ColumnMappings.Add("ProductID", "ProductID");
                    bulkCopy.ColumnMappings.Add("UnitPrice", "UnitPrice");

                    // Write readerDetail to the destination. 
                    catch (Exception ex)

                // Perform a final count on the destination 
                // tables to see how many rows were added.  
                long countEndHeader = System.Convert.ToInt32(
                Console.WriteLine("{0} rows were added to the Header table.",
                    countEndHeader - countStartHeader);
                long countEndDetail = System.Convert.ToInt32(
                Console.WriteLine("{0} rows were added to the Detail table.",
                    countEndDetail - countStartDetail);
                Console.WriteLine("Press Enter to finish.");

    private static string GetConnectionString()
        // To avoid storing the connection 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