SqlBulkCopyColumnMapping 생성자
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
SqlBulkCopyColumnMapping 클래스의 새 인스턴스를 초기화합니다.
오버로드
SqlBulkCopyColumnMapping() |
새 SqlBulkCopyColumnMapping 개체를 초기화하는 매개 변수가 없는 생성자입니다. |
SqlBulkCopyColumnMapping(Int32, Int32) |
소스 및 대상 열을 참조하는 열 서수를 사용하여 새 열 매핑을 만듭니다. |
SqlBulkCopyColumnMapping(Int32, String) |
대상 열에 대한 열 이름과 소스 열을 참조하는 열 서수를 사용하여 새 열 매핑을 만듭니다. |
SqlBulkCopyColumnMapping(String, Int32) |
대상 열에 대한 열 서수와 소스 열을 참조하는 열 이름을 사용하여 새 열 매핑을 만듭니다. |
SqlBulkCopyColumnMapping(String, String) |
소스 및 대상 열을 참조하는 열 이름을 사용하여 새 열 매핑을 만듭니다. |
SqlBulkCopyColumnMapping()
새 SqlBulkCopyColumnMapping 개체를 초기화하는 매개 변수가 없는 생성자입니다.
public:
SqlBulkCopyColumnMapping();
public SqlBulkCopyColumnMapping ();
Public Sub New ()
예제
다음 예제에서는 AdventureWorks 샘플 데이터베이스의 원본 테이블에서 동일한 데이터베이스의 대상 테이블로 데이터를 대량 복사합니다. 대상의 열 수가 원본의 열 수와 일치하지만 열 이름과 서수 위치는 일치하지 않습니다. SqlBulkCopyColumnMapping 개체는 대량 복사에 대한 열 맵을 만드는 데 사용됩니다.
중요
이 샘플은 가져올 대량 복사 샘플 설정에 설명된 대로 작업 테이블을 만들지 않은 경우 실행되지 않습니다. 이 코드는 SqlBulkCopy를 사용하는 구문을 보여 주는 용도로 제공됩니다. 원본 테이블과 대상 테이블이 동일한 SQL Server instance 있는 경우 Transact-SQL INSERT … SELECT
문을 사용하여 데이터를 복사하는 것이 더 쉽고 빠릅니다.
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;";
}
}
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
설명
이 생성자를 사용하는 경우 속성 또는 속성을 사용하여 SourceColumn 매핑의 원본을 정의하고 속성 또는 SourceOrdinal 속성을 사용하여 매핑 대상을 DestinationColumnDestinationOrdinal 정의해야 합니다.
추가 정보
적용 대상
SqlBulkCopyColumnMapping(Int32, Int32)
소스 및 대상 열을 참조하는 열 서수를 사용하여 새 열 매핑을 만듭니다.
public:
SqlBulkCopyColumnMapping(int sourceColumnOrdinal, int destinationOrdinal);
public SqlBulkCopyColumnMapping (int sourceColumnOrdinal, int destinationOrdinal);
new System.Data.SqlClient.SqlBulkCopyColumnMapping : int * int -> System.Data.SqlClient.SqlBulkCopyColumnMapping
Public Sub New (sourceColumnOrdinal As Integer, destinationOrdinal As Integer)
매개 변수
- sourceColumnOrdinal
- Int32
데이터 소스 내에 있는 소스 열의 위치입니다.
- destinationOrdinal
- Int32
대상 테이블 내에 있는 대상 열의 위치입니다.
예제
다음 예제에서는 AdventureWorks 샘플 데이터베이스의 원본 테이블에서 동일한 데이터베이스의 대상 테이블로 데이터를 대량 복사합니다. 대상의 열 수가 원본의 열 수와 일치하지만 열 이름과 서수 위치는 일치하지 않습니다. SqlBulkCopyColumnMapping 개체는 열의 서수 위치에 따라 대량 복사에 대한 열 맵을 만드는 데 사용됩니다.
중요
이 샘플은 가져올 대량 복사 샘플 설정에 설명된 대로 작업 테이블을 만들지 않은 경우 실행되지 않습니다. 이 코드는 SqlBulkCopy를 사용하는 구문을 보여 주는 용도로 제공됩니다. 원본 테이블과 대상 테이블이 동일한 SQL Server instance 있는 경우 Transact-SQL INSERT … SELECT
문을 사용하여 데이터를 복사하는 것이 더 쉽고 빠릅니다.
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;";
}
}
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
추가 정보
적용 대상
SqlBulkCopyColumnMapping(Int32, String)
대상 열에 대한 열 이름과 소스 열을 참조하는 열 서수를 사용하여 새 열 매핑을 만듭니다.
public:
SqlBulkCopyColumnMapping(int sourceColumnOrdinal, System::String ^ destinationColumn);
public SqlBulkCopyColumnMapping (int sourceColumnOrdinal, string destinationColumn);
new System.Data.SqlClient.SqlBulkCopyColumnMapping : int * string -> System.Data.SqlClient.SqlBulkCopyColumnMapping
Public Sub New (sourceColumnOrdinal As Integer, destinationColumn As String)
매개 변수
- sourceColumnOrdinal
- Int32
데이터 소스 내에 있는 소스 열의 위치입니다.
- destinationColumn
- String
대상 테이블 내에 있는 대상 열의 이름입니다.
예제
다음 예제에서는 AdventureWorks 샘플 데이터베이스의 원본 테이블에서 동일한 데이터베이스의 대상 테이블로 데이터를 대량 복사합니다. 대상의 열 수가 원본의 열 수와 일치하지만 열 이름과 서수 위치는 일치하지 않습니다. SqlBulkCopyColumnMapping 개체는 대량 복사에 대한 열 맵을 만드는 데 사용됩니다.
중요
이 샘플은 가져올 대량 복사 샘플 설정에 설명된 대로 작업 테이블을 만들지 않은 경우 실행되지 않습니다. 이 코드는 SqlBulkCopy를 사용하는 구문을 보여 주는 용도로 제공됩니다. 원본 테이블과 대상 테이블이 동일한 SQL Server instance 있는 경우 Transact-SQL INSERT … SELECT
문을 사용하여 데이터를 복사하는 것이 더 쉽고 빠릅니다.
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;";
}
}
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
추가 정보
적용 대상
SqlBulkCopyColumnMapping(String, Int32)
대상 열에 대한 열 서수와 소스 열을 참조하는 열 이름을 사용하여 새 열 매핑을 만듭니다.
public:
SqlBulkCopyColumnMapping(System::String ^ sourceColumn, int destinationOrdinal);
public SqlBulkCopyColumnMapping (string sourceColumn, int destinationOrdinal);
new System.Data.SqlClient.SqlBulkCopyColumnMapping : string * int -> System.Data.SqlClient.SqlBulkCopyColumnMapping
Public Sub New (sourceColumn As String, destinationOrdinal As Integer)
매개 변수
- sourceColumn
- String
데이터 소스 내에 있는 소스 열의 이름입니다.
- destinationOrdinal
- Int32
대상 테이블 내에 있는 대상 열의 위치입니다.
예제
다음 예제에서는 AdventureWorks 샘플 데이터베이스의 원본 테이블에서 동일한 데이터베이스의 대상 테이블로 데이터를 대량 복사합니다. 대상의 열 수가 원본의 열 수와 일치하지만 열 이름과 서수 위치는 일치하지 않습니다. SqlBulkCopyColumnMapping 개체는 대량 복사에 대한 열 맵을 만드는 데 사용됩니다.
중요
이 샘플은 가져올 대량 복사 샘플 설정에 설명된 대로 작업 테이블을 만들지 않은 경우 실행되지 않습니다. 이 코드는 SqlBulkCopy를 사용하는 구문을 보여 주는 용도로 제공됩니다. 원본 테이블과 대상 테이블이 동일한 SQL Server instance 있는 경우 Transact-SQL INSERT … SELECT
문을 사용하여 데이터를 복사하는 것이 더 쉽고 빠릅니다.
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;";
}
}
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
추가 정보
적용 대상
SqlBulkCopyColumnMapping(String, String)
소스 및 대상 열을 참조하는 열 이름을 사용하여 새 열 매핑을 만듭니다.
public:
SqlBulkCopyColumnMapping(System::String ^ sourceColumn, System::String ^ destinationColumn);
public SqlBulkCopyColumnMapping (string sourceColumn, string destinationColumn);
new System.Data.SqlClient.SqlBulkCopyColumnMapping : string * string -> System.Data.SqlClient.SqlBulkCopyColumnMapping
Public Sub New (sourceColumn As String, destinationColumn As String)
매개 변수
- sourceColumn
- String
데이터 소스 내에 있는 소스 열의 이름입니다.
- destinationColumn
- String
대상 테이블 내에 있는 대상 열의 이름입니다.
예제
다음 예제에서는 AdventureWorks 샘플 데이터베이스의 원본 테이블에서 동일한 데이터베이스의 대상 테이블로 데이터를 대량 복사합니다. 대상의 열 수가 원본의 열 수와 일치하지만 열 이름과 서수 위치는 일치하지 않습니다. SqlBulkCopyColumnMapping 개체는 대량 복사에 대한 열 맵을 만드는 데 사용됩니다.
중요
이 샘플은 가져올 대량 복사 샘플 설정에 설명된 대로 작업 테이블을 만들지 않은 경우 실행되지 않습니다. 이 코드는 SqlBulkCopy를 사용하는 구문을 보여 주는 용도로 제공됩니다. 원본 테이블과 대상 테이블이 동일한 SQL Server instance 있는 경우 Transact-SQL INSERT … SELECT
문을 사용하여 데이터를 복사하는 것이 더 쉽고 빠릅니다.
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;";
}
}
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
추가 정보
적용 대상
.NET