SqlBulkCopyColumnMappingCollection.Add メソッド

定義

オーバーロード

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 オブジェクト。

戻り値

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 がスローされます。

適用対象