SqlBulkCopyColumnMappingCollection.Add 方法      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| Add(SqlBulkCopyColumnMapping) | 
						 将指定的映射添加到 SqlBulkCopyColumnMappingCollection 中。  | 
        	
| Add(Int32, Int32) | 
						 通过使用序号指定源列和目标列,创建一个新的 SqlBulkCopyColumnMapping 并将其添加到集合中。  | 
        	
| Add(Int32, String) | 
						 通过对源列使用序号和对目标列使用字符串,创建一个新的 SqlBulkCopyColumnMapping 并将其添加到集合中。  | 
        	
| Add(String, Int32) | 
						 通过使用列名称描述源列,同时使用序号指定目标列,从而创建一个新的 SqlBulkCopyColumnMapping 并将其添加到集合中。  | 
        	
| Add(String, String) | 
						 通过使用列名称指定源列和目标列,创建一个新的 SqlBulkCopyColumnMapping 并将其添加到集合中。  | 
        	
Add(SqlBulkCopyColumnMapping)
将指定的映射添加到 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 对象。
返回
示例
以下示例将数据从“AdventureWorks”示例数据库中的源表大容量复制到同一个数据库中的目标表。 尽管目标中的列数与源中的列数匹配,但列名称和序号位置不匹配。 SqlBulkCopyColumnMapping 对象用于为大容量复制创建列映射。
重要
除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。
提供此代码是为了演示仅使用 SqlBulkCopy 时的语法。 如果源表和目标表位于同一SQL Server实例中,则使用 Transact-SQL INSERT … SELECT 语句复制数据会更轻松、更快。
// <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 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;";
    }
}
// </Snippet1>
  适用于
Add(Int32, Int32)
通过使用序号指定源列和目标列,创建一个新的 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 对象用于使用源列和目标列的序号位置为大容量复制创建列映射。
重要
除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。
提供此代码是为了演示仅使用 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)
通过对源列使用序号和对目标列使用字符串,创建一个新的 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 对象用于为大容量复制创建列映射。
重要
除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。
提供此代码是为了演示仅使用 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)
通过使用列名称描述源列,同时使用序号指定目标列,从而创建一个新的 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 对象用于为大容量复制创建列映射。
重要
除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。
提供此代码是为了演示仅使用 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)
通过使用列名称指定源列和目标列,创建一个新的 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 对象。
重要
除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。
提供此代码是为了演示仅使用 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 则会引发 。