SqlBulkCopyOptions 열거형

정의

SqlBulkCopy 인스턴스와 함께 사용할 하나 이상의 옵션을 지정하는 비트 플래그입니다.

이 열거형은 멤버 값의 비트 조합을 지원합니다.

public enum class SqlBulkCopyOptions
[System.Flags]
public enum SqlBulkCopyOptions
[<System.Flags>]
type SqlBulkCopyOptions = 
Public Enum SqlBulkCopyOptions
상속
SqlBulkCopyOptions
특성

필드

AllowEncryptedValueModifications 64

지정한 경우 AllowEncryptedValueModifications 를 사용하면 데이터 암호를 해독하지 않고도 테이블 또는 데이터베이스 간에 암호화된 데이터를 대량으로 복사할 수 있습니다. 일반적으로 애플리케이션은 데이터를 해독하지 않고 한 테이블에서 암호화된 열을 선택한(앱은 열 암호화 설정 키워드가 사용하지 않도록 설정된 상태에서 데이터베이스에 연결함) 다음, 이 옵션을 사용하여 여전히 암호돠된 데이터를 대량 복사합니다. 자세한 내용은 Always Encrypted를 참조하세요.

AllowEncryptedValueModifications를 지정하면 데이터가 실제로 암호화된 경우 드라이버가 검사 않거나 대상 열과 동일한 암호화 유형, 알고리즘 및 키를 사용하여 올바르게 암호화된 경우 데이터베이스가 손상될 수 있으므로 주의해야 합니다.

CheckConstraints 2

데이터가 삽입되는 동안 제약 조건을 확인합니다. 기본적으로 제약 조건은 확인하지 않습니다.

Default 0

모든 옵션의 기본값을 사용합니다.

FireTriggers 16

지정한 경우 서버에서 데이터베이스에 삽입할 행에 대해 삽입 트리거를 발생하도록 합니다.

KeepIdentity 1

원본 ID 값을 유지합니다. 지정되지 않은 경우 대상에서 ID 값을 지정합니다.

KeepNulls 8

기본값에 대한 설정에 관계없이 대상 테이블에서 null 값을 유지합니다. 지정되지 않은 경우 Null 값은 기본값으로 대체됩니다(해당하는 경우).

TableLock 4

대량 복사 작업의 기간 동안 대량 업데이트 잠금을 획득합니다. 지정하지 않으면 행 잠금이 사용됩니다.

UseInternalTransaction 32

지정한 경우 대량 복사 작업의 각 일괄 처리가 트랜잭션 내에서 발생됩니다. 이 옵션을 지정하고 SqlTransaction 개체도 생성자에 제공하는 경우 ArgumentException이 발생합니다.

예제

다음 콘솔 애플리케이션에는 원본 테이블의 id 열에서 각 행의 id 열에 대 한 새 값을 생성 하는 대신 대상 테이블의 해당 열 값을 복사 하는 대량 로드를 수행 하는 방법을 보여 줍니다.

옵션이 대량 로드의 작동 방식을 변경하는 방법을 확인하려면 dbo를 사용하여 샘플을 실행합니다 . BulkCopyDemoMatchingColumns 테이블이 비어 있습니다 . 모든 행은 원본에서 로드됩니다. 다음으로 테이블을 비우지 않고 샘플을 다시 실행합니다. 예외가 throw되고 코드는 기본 키 위반으로 인해 행이 추가되지 않았다는 메시지를 콘솔 창에 씁니다.

중요

이 샘플은 가져올 대량 복사 샘플 설정에 설명된 대로 작업 테이블을 만들지 않은 경우 실행되지 않습니다. 이 코드는 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.BulkCopyDemoMatchingColumns;",
                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();

            // Create the SqlBulkCopy object using a connection string
            // and the KeepIdentity option.
            // In the real world you would not use SqlBulkCopy to move
            // data from one table to the other in the same database.
            using (SqlBulkCopy bulkCopy =
                new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
            {
                bulkCopy.DestinationTableName =
                    "dbo.BulkCopyDemoMatchingColumns";

                try
                {
                    // Write from the source to the destination.
                    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.BulkCopyDemoMatchingColumns;", _
                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

            ' Create the SqlBulkCopy object using a connection string 
            ' and the KeepIdentity option. 
            ' In the real world you would not use SqlBulkCopy to move
            ' data from one table to the other in the same database.
            Using bulkCopy As SqlBulkCopy = _
              New SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity)
                bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns"

                Try
                    ' Write from the source to the destination.
                    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

설명

instance 생성할 때 열거형을 SqlBulkCopy 사용하여 SqlBulkCopyOptions 해당 instance 메서드의 WriteToServer 동작 방식을 변경할 수 있습니다.

적용 대상

추가 정보