语言

SqlBulkCopyColumnMappingCollection.Add 方法

定义

重载

名称 说明
Add(SqlBulkCopyColumnMapping)

将指定的映射添加到 SqlBulkCopyColumnMappingCollection.

Add(Int32, Int32)

创建一个新 SqlBulkCopyColumnMapping 项并将其添加到集合,使用序号指定源列和目标列。

Add(Int32, String)

使用源列的序号和目标列的字符串创建一个新 SqlBulkCopyColumnMapping 项并将其添加到集合。

Add(String, Int32)

创建一个新 SqlBulkCopyColumnMapping 项并将其添加到集合,使用列名描述源列和序号来指定目标列。

Add(String, String)

创建一个新 SqlBulkCopyColumnMapping 项并将其添加到集合中,使用列名指定源列和目标列。

Add(SqlBulkCopyColumnMapping)

Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs

将指定的映射添加到 SqlBulkCopyColumnMappingCollection.

public:
 Microsoft::Data::SqlClient::SqlBulkCopyColumnMapping ^ Add(Microsoft::Data::SqlClient::SqlBulkCopyColumnMapping ^ bulkCopyColumnMapping);
public Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping Add(Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping bulkCopyColumnMapping);
member this.Add : Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping -> Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping
Public Function Add (bulkCopyColumnMapping As SqlBulkCopyColumnMapping) As SqlBulkCopyColumnMapping

参数

bulkCopyColumnMapping
SqlBulkCopyColumnMapping

SqlBulkCopyColumnMapping描述要添加到集合中的映射的对象。

返回

一个 SqlBulkCopyColumnMapping 对象。

示例

以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 尽管目标中的列数与源中的列数匹配,但列名和序号位置不匹配。 SqlBulkCopyColumnMapping 对象用于为大容量复制创建列映射。

Important

除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。 提供此代码是为了演示仅使用 SqlBulkCopy 时的语法。 如果源表和目标表位于同一 SQL Server 实例中,则使用 Transact-SQL INSERT … SELECT 语句复制数据更容易、更快。

namespace SqlBulkCopy_ColumnMapping;

// <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.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 mapNumber =
                    new SqlBulkCopyColumnMapping("ProductNumber", "ProdNum");
                bulkCopy.ColumnMappings.Add(mapNumber);

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

以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 尽管目标中的列数与源中的列数匹配,但列名和序号位置不匹配。 SqlBulkCopyColumnMapping 对象用于为大容量复制创建列映射。

Important

除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。

提供此代码是为了演示仅使用 SqlBulkCopy 时的语法。 如果源表和目标表位于同一 SQL Server 实例中,则使用 Transact-SQL INSERT … SELECT 语句复制数据更容易、更快。

namespace SqlBulkCopy_ColumnMapping;

// <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.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 mapNumber =
                    new SqlBulkCopyColumnMapping("ProductNumber", "ProdNum");
                bulkCopy.ColumnMappings.Add(mapNumber);

                // 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(Int32, Int32)

Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs

创建一个新 SqlBulkCopyColumnMapping 项并将其添加到集合,使用序号指定源列和目标列。

public:
 Microsoft::Data::SqlClient::SqlBulkCopyColumnMapping ^ Add(int sourceColumnIndex, int destinationColumnIndex);
public Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping Add(int sourceColumnIndex, int destinationColumnIndex);
member this.Add : int * int -> Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping
Public Function Add (sourceColumnIndex As Integer, destinationColumnIndex As Integer) As SqlBulkCopyColumnMapping

参数

sourceColumnIndex
Int32

数据源中源列的序号位置。

destinationColumnIndex
Int32

目标表中目标列的序号位置。

返回

列映射。

示例

以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 尽管目标中的列数与源中的列数匹配,但列名和序号位置不匹配。 SqlBulkCopyColumnMapping 对象用于使用源列和目标列的序号位置为大容量复制创建列映射。

Important

除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。

提供此代码是为了演示仅使用 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.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";

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

注解

集合中的映射必须统一:所有整数/整数对、所有字符串/字符串对、所有整数/字符串对或所有字符串/整数对。 如果尝试添加与集合中已有的其他人不同的映射,则会引发一个 InvalidOperationException 映射。

适用于

Add(Int32, String)

Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs

使用源列的序号和目标列的字符串创建一个新 SqlBulkCopyColumnMapping 项并将其添加到集合。

public:
 Microsoft::Data::SqlClient::SqlBulkCopyColumnMapping ^ Add(int sourceColumnIndex, System::String ^ destinationColumn);
public Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping Add(int sourceColumnIndex, string destinationColumn);
member this.Add : int * string -> Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping
Public Function Add (sourceColumnIndex As Integer, destinationColumn As String) As SqlBulkCopyColumnMapping

参数

sourceColumnIndex
Int32

数据源中源列的序号位置。

destinationColumn
String

目标表中的目标列的名称。

返回

列映射。

示例

以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 尽管目标中的列数与源中的列数匹配,但列名和序号位置不匹配。 SqlBulkCopyColumnMapping 对象用于为大容量复制创建列映射。

Important

除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。

提供此代码是为了演示仅使用 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.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";

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

注解

集合中的映射必须统一:所有整数/整数对、所有字符串/字符串对、所有整数/字符串对或所有字符串/整数对。 如果尝试添加与集合中已有的其他人不同的映射,则会引发一个 InvalidOperationException 映射。

适用于

Add(String, Int32)

Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs

创建一个新 SqlBulkCopyColumnMapping 项并将其添加到集合,使用列名描述源列和序号来指定目标列。

public:
 Microsoft::Data::SqlClient::SqlBulkCopyColumnMapping ^ Add(System::String ^ sourceColumn, int destinationColumnIndex);
public Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping Add(string sourceColumn, int destinationColumnIndex);
member this.Add : string * int -> Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping
Public Function Add (sourceColumn As String, destinationColumnIndex As Integer) As SqlBulkCopyColumnMapping

参数

sourceColumn
String

数据源中的源列的名称。

destinationColumnIndex
Int32

目标表中目标列的序号位置。

返回

列映射。

示例

以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 尽管目标中的列数与源中的列数匹配,但列名和序号位置不匹配。 SqlBulkCopyColumnMapping 对象用于为大容量复制创建列映射。

Important

除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。

提供此代码是为了演示仅使用 SqlBulkCopy 时的语法。 如果源表和目标表位于同一 SQL Server 实例中,则使用 Transact-SQL INSERT … SELECT 语句复制数据更容易、更快。

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

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

注解

集合中的映射必须统一:所有整数/整数对、所有字符串/字符串对、所有整数/字符串对或所有字符串/整数对。 如果尝试添加与集合中已有的其他人不同的映射,则会引发一个 InvalidOperationException 映射。

适用于

Add(String, String)

Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs
Source:
SqlBulkCopyColumnMappingCollection.cs

创建一个新 SqlBulkCopyColumnMapping 项并将其添加到集合中,使用列名指定源列和目标列。

public:
 Microsoft::Data::SqlClient::SqlBulkCopyColumnMapping ^ Add(System::String ^ sourceColumn, System::String ^ destinationColumn);
public Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping Add(string sourceColumn, string destinationColumn);
member this.Add : string * string -> Microsoft.Data.SqlClient.SqlBulkCopyColumnMapping
Public Function Add (sourceColumn As String, destinationColumn As String) As SqlBulkCopyColumnMapping

参数

sourceColumn
String

数据源中的源列的名称。

destinationColumn
String

目标表中的目标列的名称。

返回

列映射。

示例

以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 尽管目标中的列数与源中的列数匹配,但列名和序号位置不匹配。 该代码通过指定列名来创建 SqlBulkCopyColumnMapping 对象。

Important

除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。

提供此代码是为了演示仅使用 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.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";

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

注解

集合中的映射必须统一:所有整数/整数对、所有字符串/字符串对、所有整数/字符串对或所有字符串/整数对。 如果尝试添加与集合中已有的其他人不同的映射,则会引发一个 InvalidOperationException 映射。

适用于