SqlBulkCopyOptions 列挙型

定義

SqlBulkCopy のインスタンスで使用する 1 つまたは複数のオプションを指定するビットごとのフラグ。

この列挙体は、メンバー値のビットごとの組み合わせをサポートしています。

public enum class SqlBulkCopyOptions
[System.Flags]
public enum SqlBulkCopyOptions
[<System.Flags>]
type SqlBulkCopyOptions = 
Public Enum SqlBulkCopyOptions
継承
SqlBulkCopyOptions
属性

フィールド

AllowEncryptedValueModifications 64

AllowEncryptedValueModifications を指定すると、データの暗号化を解除することなく、テーブルまたはデータベース間で暗号化されたデータを一括コピーできます。 一般的に、アプリケーションはデータを復号せずに 1 つのテーブルの暗号化された列からデータを選択し (アプリは、列の暗号化設定キーワードを無効に設定しているデータベースに接続します)、それからこのオプションを使用し、依然として暗号化されたままのデータを一括挿入します。 詳細については、「 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 テーブルが空です。 すべての行がソースから読み込まれます。 次に、テーブルを空にせずにサンプルをもう一度実行します。 例外がスローされ、主キー違反のために行が追加されなかったことを通知するメッセージがコンソール ウィンドウに書き込まれます。

重要

このサンプルは、「バルク コピー サンプルのセットアップ」で説明されているように作業テーブルを作成してからでないと動作しません。 このコードでは、SqlBulkCopy だけを使用した構文について説明します。 ソース テーブルと変換先テーブルが同じSQL Server インスタンスにある場合は、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

注釈

インスタンスを構築するときに 列挙を SqlBulkCopyOptions 使用して、 SqlBulkCopy そのインスタンスのメソッドの WriteToServer 動作を変更できます。

適用対象

こちらもご覧ください