SqlBulkCopyColumnMapping クラス
アセンブリ: System.Data (system.data.dll 内)


データ ソースとコピー先テーブルとの間の対応付けは、列マップによって定義されます。
マップが定義されていない場合、つまり、ColumnMappings コレクションが空の場合、列の序数位置に基づいて暗黙的に対応付けされます。そのためには、コピー元とコピー先のスキーマが一致していることが必要です。それ以外の場合、InvalidOperationException がスローされます。
ColumnMappings コレクションが空ではない場合、データ ソース内のすべての列を指定する必要はありません。コレクションでマップされていない列は無視されます。
コピー元とコピー先の列は、名前で参照することも、序数で参照することもできます。また、名前による列の参照と、序数による列の参照が、同じマップ コレクション内で混在していてもかまいません。

次の例は、AdventureWorks サンプル データベースのソース テーブルからデータを、同じデータベースの別のテーブルに一括コピーします。コピー先の列数はコピー元の列数と一致し、コピー先の各列は対応するコピー元の列と同じ序数位置にありますが、列名が一致していません。SqlBulkCopyColumnMapping オブジェクトが、一括コピーの列マップを作成するために使用されます。
![]() |
---|
このサンプルを実行するには、あらかじめ、「バルク コピーの例のためのテーブルの作成」の説明に従って作業テーブルを作成しておく必要があります。このコードは、SqlBulkCopy を使用する構文を示すためだけに提供されています。同じ SQL Server インスタンスにコピー元テーブルとコピー先テーブルが存在する場合、Transact-SQL の INSERT … SELECT ステートメントを使用した方が容易かつ迅速にデータをコピーできます。 |
Imports System.Data.SqlClient Module Module1 Sub Main() Dim connectionString As String = GetConnectionString() ' Open a connection to the AdventureWorks database. Using sourceConnection As SqlConnection = _ New SqlConnection(connectionString) sourceConnection.Open() ' Perform an initial count on the destination table. Dim commandRowCount As New SqlCommand( _ "SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns;", _ sourceConnection) Dim countStart As Long = _ System.Convert.ToInt32(commandRowCount.ExecuteScalar()) Console.WriteLine("Starting row count = {0}", countStart) ' Get data from the source table as a SqlDataReader. Dim commandSourceData As SqlCommand = New SqlCommand( _ "SELECT ProductID, Name, ProductNumber " & _ "FROM Production.Product;", sourceConnection) Dim reader As SqlDataReader = commandSourceData.ExecuteReader ' Set up the bulk copy object. Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(connectionString) bulkCopy.DestinationTableName = _ "dbo.BulkCopyDemoDifferentColumns" ' Set up the column mappings by name. Dim mapID As New _ SqlBulkCopyColumnMapping("ProductID", "ProdID") bulkCopy.ColumnMappings.Add(mapID) Dim mapName As New _ SqlBulkCopyColumnMapping("Name", "ProdName") bulkCopy.ColumnMappings.Add(mapName) Dim mapMumber As New _ SqlBulkCopyColumnMapping("ProductNumber", "ProdNum") bulkCopy.ColumnMappings.Add(mapMumber) ' Write from the source to the destination. Try bulkCopy.WriteToServer(reader) Catch ex As Exception Console.WriteLine(ex.Message) Finally ' Close the SqlDataReader. The SqlBulkCopy ' object is automatically closed at the end ' of the Using block. reader.Close() End Try End Using ' Perform a final count on the destination table ' to see how many rows were added. Dim countEnd As Long = _ 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() End Using End Sub Private Function GetConnectionString() As String ' 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;" End Function End Module
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)) { 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;"; } }

System.Data.SqlClient.SqlBulkCopyColumnMapping


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SqlBulkCopyColumnMapping コンストラクタ ()
アセンブリ: System.Data (system.data.dll 内)


このコンストラクタでマップを定義する場合は、SourceColumn プロパティまたは SourceOrdinal プロパティを使用してコピー元を定義し、DestinationColumn プロパティまたは DestinationOrdinal プロパティを使用してコピー先を定義する必要があります。

次の例は、AdventureWorks サンプル データベースのソース テーブルからデータを、同じデータベースの別のテーブルに一括コピーします。コピー先の列数はコピー元の列数と一致しますが、列名と序数位置が一致していません。SqlBulkCopyColumnMapping オブジェクトが、一括コピーの列マップを作成するために使用されます。
![]() |
---|
このサンプルを実行するには、あらかじめ、「バルク コピーの例のためのテーブルの作成」の説明に従って作業テーブルを作成しておく必要があります。このコードは、SqlBulkCopy を使用する構文を示すためだけに提供されています。同じ SQL Server インスタンスにコピー元テーブルとコピー先テーブルが存在する場合、Transact-SQL の INSERT … SELECT ステートメントを使用した方が容易かつ迅速にデータをコピーできます。 |
Imports System.Data.SqlClient Module Module1 Sub Main() Dim connectionString As String = GetConnectionString() ' Open a connection to the AdventureWorks database. Using sourceConnection As SqlConnection = _ New SqlConnection(connectionString) sourceConnection.Open() ' Perform an initial count on the destination table. Dim commandRowCount As New SqlCommand( _ "SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns;", _ sourceConnection) Dim countStart As Long = _ System.Convert.ToInt32(commandRowCount.ExecuteScalar()) Console.WriteLine("Starting row count = {0}", countStart) ' Get data from the source table as a SqlDataReader. Dim commandSourceData As SqlCommand = New SqlCommand( _ "SELECT ProductID, Name, ProductNumber " & _ "FROM Production.Product;", sourceConnection) Dim reader As SqlDataReader = commandSourceData.ExecuteReader ' Set up the bulk copy object. Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(connectionString) bulkCopy.DestinationTableName = _ "dbo.BulkCopyDemoDifferentColumns" ' Set up the column mappings by name. Dim mapID As New _ SqlBulkCopyColumnMapping("ProductID", "ProdID") bulkCopy.ColumnMappings.Add(mapID) Dim mapName As New _ SqlBulkCopyColumnMapping("Name", "ProdName") bulkCopy.ColumnMappings.Add(mapName) Dim mapMumber As New _ SqlBulkCopyColumnMapping("ProductNumber", "ProdNum") bulkCopy.ColumnMappings.Add(mapMumber) ' Write from the source to the destination. Try bulkCopy.WriteToServer(reader) Catch ex As Exception Console.WriteLine(ex.Message) Finally ' Close the SqlDataReader. The SqlBulkCopy ' object is automatically closed at the end ' of the Using block. reader.Close() End Try End Using ' Perform a final count on the destination table ' to see how many rows were added. Dim countEnd As Long = _ 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() End Using End Sub Private Function GetConnectionString() As String ' 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;" End Function End Module
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)) { 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;"; } }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SqlBulkCopyColumnMapping コンストラクタ (Int32, Int32)
アセンブリ: System.Data (system.data.dll 内)

Dim sourceColumnOrdinal As Integer Dim destinationOrdinal As Integer Dim instance As New SqlBulkCopyColumnMapping(sourceColumnOrdinal, destinationOrdinal)

次の例は、AdventureWorks サンプル データベースのソース テーブルからデータを、同じデータベースの別のテーブルに一括コピーします。コピー先の列数はコピー元の列数と一致しますが、列名と序数位置が一致していません。SqlBulkCopyColumnMapping オブジェクトが、列の序数位置に基づいて一括コピーの列マップを作成するために使用されます。
![]() |
---|
このサンプルを実行するには、あらかじめ、「バルク コピーの例のためのテーブルの作成」の説明に従って作業テーブルを作成しておく必要があります。このコードは、SqlBulkCopy を使用する構文を示すためだけに提供されています。同じ SQL Server インスタンスにコピー元テーブルとコピー先テーブルが存在する場合、Transact-SQL の INSERT … SELECT ステートメントを使用した方が容易かつ迅速にデータをコピーできます。 |
Imports System.Data.SqlClient Module Module1 Sub Main() Dim connectionString As String = GetConnectionString() ' Open a connection to the AdventureWorks database. Using sourceConnection As SqlConnection = _ New SqlConnection(connectionString) sourceConnection.Open() ' Perform an initial count on the destination table. Dim commandRowCount As New SqlCommand( _ "SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns;", _ sourceConnection) Dim countStart As Long = _ System.Convert.ToInt32(commandRowCount.ExecuteScalar()) Console.WriteLine("Starting row count = {0}", countStart) ' Get data from the source table as a SqlDataReader. Dim commandSourceData As SqlCommand = New SqlCommand( _ "SELECT ProductID, Name, ProductNumber " & _ "FROM Production.Product;", sourceConnection) Dim reader As SqlDataReader = commandSourceData.ExecuteReader ' Set up the bulk copy object. Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(connectionString) bulkCopy.DestinationTableName = _ "dbo.BulkCopyDemoDifferentColumns" ' Set up the column mappings by ordinal. Dim columnMapID As New _ SqlBulkCopyColumnMapping(0, 0) bulkCopy.ColumnMappings.Add(columnMapID) Dim columnMapName As New _ SqlBulkCopyColumnMapping(1, 2) bulkCopy.ColumnMappings.Add(columnMapName) Dim columnMapNumber As New _ SqlBulkCopyColumnMapping(2, 1) bulkCopy.ColumnMappings.Add(columnMapNumber) ' Write from the source to the destination. Try bulkCopy.WriteToServer(reader) Catch ex As Exception Console.WriteLine(ex.Message) Finally ' Close the SqlDataReader. The SqlBulkCopy ' object is automatically closed at the end ' of the Using block. reader.Close() End Try End Using ' Perform a final count on the destination table ' to see how many rows were added. Dim countEnd As Long = _ 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() End Using End Sub Private Function GetConnectionString() As String ' 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;" End Function End Module
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)) { 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 ordinal. SqlBulkCopyColumnMapping columnMapID = new SqlBulkCopyColumnMapping(0, 0); bulkCopy.ColumnMappings.Add(columnMapID); SqlBulkCopyColumnMapping columnMapName = new SqlBulkCopyColumnMapping(1, 2); bulkCopy.ColumnMappings.Add(columnMapName); SqlBulkCopyColumnMapping columnMapNumber = new SqlBulkCopyColumnMapping(2, 1); bulkCopy.ColumnMappings.Add(columnMapNumber); // 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;"; } }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SqlBulkCopyColumnMapping コンストラクタ (String, Int32)
アセンブリ: System.Data (system.data.dll 内)

Dim sourceColumn As String Dim destinationOrdinal As Integer Dim instance As New SqlBulkCopyColumnMapping(sourceColumn, destinationOrdinal)

次の例は、AdventureWorks サンプル データベースのソース テーブルからデータを、同じデータベースの別のテーブルに一括コピーします。コピー先の列数はコピー元の列数と一致しますが、列名と序数位置が一致していません。SqlBulkCopyColumnMapping オブジェクトが、一括コピーの列マップを作成するために使用されます。
![]() |
---|
このサンプルを実行するには、あらかじめ、「バルク コピーの例のためのテーブルの作成」の説明に従って作業テーブルを作成しておく必要があります。このコードは、SqlBulkCopy を使用する構文を示すためだけに提供されています。同じ SQL Server インスタンスにコピー元テーブルとコピー先テーブルが存在する場合、Transact-SQL の INSERT … SELECT ステートメントを使用した方が容易かつ迅速にデータをコピーできます。 |
Imports System.Data.SqlClient Module Module1 Sub Main() Dim connectionString As String = GetConnectionString() ' Open a connection to the AdventureWorks database. Using sourceConnection As SqlConnection = _ New SqlConnection(connectionString) sourceConnection.Open() ' Perform an initial count on the destination table. Dim commandRowCount As New SqlCommand( _ "SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns;", _ sourceConnection) Dim countStart As Long = _ System.Convert.ToInt32(commandRowCount.ExecuteScalar()) Console.WriteLine("Starting row count = {0}", countStart) ' Get data from the source table as a SqlDataReader. Dim commandSourceData As SqlCommand = New SqlCommand( _ "SELECT ProductID, Name, ProductNumber " & _ "FROM Production.Product;", sourceConnection) Dim reader As SqlDataReader = commandSourceData.ExecuteReader ' Set up the bulk copy object. Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(connectionString) bulkCopy.DestinationTableName = _ "dbo.BulkCopyDemoDifferentColumns" ' Set up the column mappings by name and ordinal. Dim columnMapID As New _ SqlBulkCopyColumnMapping("ProductID", 0) bulkCopy.ColumnMappings.Add(columnMapID) Dim columnMapName As New _ SqlBulkCopyColumnMapping("Name", 2) bulkCopy.ColumnMappings.Add(columnMapName) Dim columnMapNumber As New _ SqlBulkCopyColumnMapping("ProductNumber", 1) bulkCopy.ColumnMappings.Add(columnMapNumber) ' Write from the source to the destination. Try bulkCopy.WriteToServer(reader) Catch ex As Exception Console.WriteLine(ex.Message) Finally ' Close the SqlDataReader. The SqlBulkCopy ' object is automatically closed at the end ' of the Using block. reader.Close() End Try End Using ' Perform a final count on the destination table ' to see how many rows were added. Dim countEnd As Long = _ 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() End Using End Sub Private Function GetConnectionString() As String ' 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;" End Function End Module
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)) { 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 and ordinal. SqlBulkCopyColumnMapping columnMapID = new SqlBulkCopyColumnMapping("ProductID", 0); bulkCopy.ColumnMappings.Add(columnMapID); SqlBulkCopyColumnMapping columnMapName = new SqlBulkCopyColumnMapping("Name", 2); bulkCopy.ColumnMappings.Add(columnMapName); SqlBulkCopyColumnMapping columnMapNumber = new SqlBulkCopyColumnMapping("ProductNumber", 1); bulkCopy.ColumnMappings.Add(columnMapNumber); // 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;"; } }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SqlBulkCopyColumnMapping コンストラクタ (Int32, String)
アセンブリ: System.Data (system.data.dll 内)

Dim sourceColumnOrdinal As Integer Dim destinationColumn As String Dim instance As New SqlBulkCopyColumnMapping(sourceColumnOrdinal, destinationColumn)

次の例は、AdventureWorks サンプル データベースのソース テーブルからデータを、同じデータベースの別のテーブルに一括コピーします。コピー先の列数はコピー元の列数と一致しますが、列名と序数位置が一致していません。SqlBulkCopyColumnMapping オブジェクトが、一括コピーの列マップを作成するために使用されます。
![]() |
---|
このサンプルを実行するには、あらかじめ、「バルク コピーの例のためのテーブルの作成」の説明に従って作業テーブルを作成しておく必要があります。このコードは、SqlBulkCopy を使用する構文を示すためだけに提供されています。同じ SQL Server インスタンスにコピー元テーブルとコピー先テーブルが存在する場合、Transact-SQL の INSERT … SELECT ステートメントを使用した方が容易かつ迅速にデータをコピーできます。 |
Imports System.Data.SqlClient Module Module1 Sub Main() Dim connectionString As String = GetConnectionString() ' Open a connection to the AdventureWorks database. Using sourceConnection As SqlConnection = _ New SqlConnection(connectionString) sourceConnection.Open() ' Perform an initial count on the destination table. Dim commandRowCount As New SqlCommand( _ "SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns;", _ sourceConnection) Dim countStart As Long = _ System.Convert.ToInt32(commandRowCount.ExecuteScalar()) Console.WriteLine("Starting row count = {0}", countStart) ' Get data from the source table as a SqlDataReader. Dim commandSourceData As SqlCommand = New SqlCommand( _ "SELECT ProductID, Name, ProductNumber " & _ "FROM Production.Product;", sourceConnection) Dim reader As SqlDataReader = commandSourceData.ExecuteReader ' Set up the bulk copy object. Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(connectionString) bulkCopy.DestinationTableName = _ "dbo.BulkCopyDemoDifferentColumns" ' Set up the column mappings by ordinal and name. Dim columnMapID As New _ SqlBulkCopyColumnMapping(0, "ProdID") bulkCopy.ColumnMappings.Add(columnMapID) Dim columnMapName As New _ SqlBulkCopyColumnMapping(1, "ProdName") bulkCopy.ColumnMappings.Add(columnMapName) Dim columnMapNumber As New _ SqlBulkCopyColumnMapping(2, "ProdNum") bulkCopy.ColumnMappings.Add(columnMapNumber) ' Write from the source to the destination. Try bulkCopy.WriteToServer(reader) Catch ex As Exception Console.WriteLine(ex.Message) Finally ' Close the SqlDataReader. The SqlBulkCopy ' object is automatically closed at the end ' of the Using block. reader.Close() End Try End Using ' Perform a final count on the destination table ' to see how many rows were added. Dim countEnd As Long = _ 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() End Using End Sub Private Function GetConnectionString() As String ' 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;" End Function End Module
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)) { 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 ordinal and name. SqlBulkCopyColumnMapping columnMapID = new SqlBulkCopyColumnMapping(0, "ProdID"); bulkCopy.ColumnMappings.Add(columnMapID); SqlBulkCopyColumnMapping columnMapName = new SqlBulkCopyColumnMapping(1, "ProdName"); bulkCopy.ColumnMappings.Add(columnMapName); SqlBulkCopyColumnMapping columnMapNumber = new SqlBulkCopyColumnMapping(2, "ProdNum"); bulkCopy.ColumnMappings.Add(columnMapNumber); // 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;"; } }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SqlBulkCopyColumnMapping コンストラクタ (String, String)
アセンブリ: System.Data (system.data.dll 内)

Dim sourceColumn As String Dim destinationColumn As String Dim instance As New SqlBulkCopyColumnMapping(sourceColumn, destinationColumn)

次の例は、AdventureWorks サンプル データベースのソース テーブルからデータを、同じデータベースの別のテーブルに一括コピーします。コピー先の列数はコピー元の列数と一致しますが、列名と序数位置が一致していません。SqlBulkCopyColumnMapping オブジェクトが、一括コピーの列マップを作成するために使用されます。
![]() |
---|
このサンプルを実行するには、あらかじめ、「バルク コピーの例のためのテーブルの作成」の説明に従って作業テーブルを作成しておく必要があります。このコードは、SqlBulkCopy を使用する構文を示すためだけに提供されています。同じ SQL Server インスタンスにコピー元テーブルとコピー先テーブルが存在する場合、Transact-SQL の INSERT … SELECT ステートメントを使用した方が容易かつ迅速にデータをコピーできます。 |
Imports System.Data.SqlClient Module Module1 Sub Main() Dim connectionString As String = GetConnectionString() ' Open a connection to the AdventureWorks database. Using sourceConnection As SqlConnection = _ New SqlConnection(connectionString) sourceConnection.Open() ' Perform an initial count on the destination table. Dim commandRowCount As New SqlCommand( _ "SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns;", _ sourceConnection) Dim countStart As Long = _ System.Convert.ToInt32(commandRowCount.ExecuteScalar()) Console.WriteLine("Starting row count = {0}", countStart) ' Get data from the source table as a SqlDataReader. Dim commandSourceData As SqlCommand = New SqlCommand( _ "SELECT ProductID, Name, ProductNumber " & _ "FROM Production.Product;", sourceConnection) Dim reader As SqlDataReader = commandSourceData.ExecuteReader ' Set up the bulk copy object. Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(connectionString) bulkCopy.DestinationTableName = _ "dbo.BulkCopyDemoDifferentColumns" ' Set up the column mappings by name. Dim mapID As New _ SqlBulkCopyColumnMapping("ProductID", "ProdID") bulkCopy.ColumnMappings.Add(mapID) Dim mapName As New _ SqlBulkCopyColumnMapping("Name", "ProdName") bulkCopy.ColumnMappings.Add(mapName) Dim mapMumber As New _ SqlBulkCopyColumnMapping("ProductNumber", "ProdNum") bulkCopy.ColumnMappings.Add(mapMumber) ' Write from the source to the destination. Try bulkCopy.WriteToServer(reader) Catch ex As Exception Console.WriteLine(ex.Message) Finally ' Close the SqlDataReader. The SqlBulkCopy ' object is automatically closed at the end ' of the Using block. reader.Close() End Try End Using ' Perform a final count on the destination table ' to see how many rows were added. Dim countEnd As Long = _ 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() End Using End Sub Private Function GetConnectionString() As String ' 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;" End Function End Module
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)) { 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;"; } }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SqlBulkCopyColumnMapping コンストラクタ

名前 | 説明 |
---|---|
SqlBulkCopyColumnMapping () | 新しい SqlBulkCopyColumnMapping オブジェクトを初期化する既定のコンストラクタ。 |
SqlBulkCopyColumnMapping (Int32, Int32) | コピー元とコピー先の列をどちらも序数で参照して、新しい列マップを作成します。 |
SqlBulkCopyColumnMapping (Int32, String) | コピー元を列の序数で、コピー先を列の名前で参照して、新しい列マップを作成します。 |
SqlBulkCopyColumnMapping (String, Int32) | コピー元を列名で、コピー先を列の序数で参照して、新しい列マップを作成します。 |
SqlBulkCopyColumnMapping (String, String) | コピー元とコピー先の列をどちらも列名で参照して、新しい列マップを作成します。 |

SqlBulkCopyColumnMapping プロパティ

名前 | 説明 | |
---|---|---|
![]() | DestinationColumn | コピー先データベース テーブルにおけるマップ対象の列名。 |
![]() | DestinationOrdinal | コピー先テーブルにおけるコピー先列の序数値。 |
![]() | SourceColumn | データ ソースにおけるマップ対象の列名。 |
![]() | SourceOrdinal | データ ソースにおけるコピー元列の序数位置。 |

SqlBulkCopyColumnMapping メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

SqlBulkCopyColumnMapping メンバ
SqlBulkCopy インスタンスのデータ ソースと、インスタンスのコピー先テーブル間の列のマップを定義します。
SqlBulkCopyColumnMapping データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | DestinationColumn | コピー先データベース テーブルにおけるマップ対象の列名。 |
![]() | DestinationOrdinal | コピー先テーブルにおけるコピー先列の序数値。 |
![]() | SourceColumn | データ ソースにおけるマップ対象の列名。 |
![]() | SourceOrdinal | データ ソースにおけるコピー元列の序数位置。 |

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

Weblioに収録されているすべての辞書からSqlBulkCopyColumnMappingを検索する場合は、下記のリンクをクリックしてください。

- SqlBulkCopyColumnMappingのページへのリンク