Language

SqlBulkCopy.NotifyAfter プロパティ

定義

通知イベントを生成する前に処理する行の数を定義します。

public:
 property int NotifyAfter { int get(); void set(int value); };
public int NotifyAfter { get; set; }
member this.NotifyAfter : int with get, set
Public Property NotifyAfter As Integer

プロパティ値

NotifyAfter プロパティの整数値。プロパティが設定されていない場合は 0。

例

次のコンソール アプリケーションは、既に開いている接続を使用してデータを一括読み込みする方法を示しています。 プロパティは NotifyAfter 、テーブルにコピーされた 50 行ごとにイベント ハンドラーが呼び出されるように設定されます。

この例では、最初に接続を使用して、SQL Server テーブルからインスタンスにデータを読み取る必要がありますSqlDataReader。 その後、2 つ目の接続が開き、データが一括コピーされます。 ソース データは、SQL Serverに配置する必要はありません。に読み込んだり、 にIDataReader読み込んだりできる任意のデータ ソースをDataTable使用できます。

重要

このサンプルは、「バルク コピー サンプルのセットアップ」で説明されているように作業テーブルを作成してからでないと動作しません。 このコードでは、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.BulkCopyDemoMatchingColumns;",
                sourceConnection);
            long countStart = System.Convert.ToInt32(
                commandRowCount.ExecuteScalar());
            Console.WriteLine("NotifyAfter Sample");
            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. 
            // 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))
            {
                bulkCopy.DestinationTableName =
                    "dbo.BulkCopyDemoMatchingColumns";

                // Set up the event handler to notify after 50 rows.
                bulkCopy.SqlRowsCopied +=
                    new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
                bulkCopy.NotifyAfter = 50;

                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 void OnSqlRowsCopied(
        object sender, SqlRowsCopiedEventArgs e)
    {
        Console.WriteLine("Copied {0} so far...", e.RowsCopied);
    }
    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;";
    }
}

注釈

このプロパティは、一括コピー操作の進行状況を示すユーザー インターフェイス コンポーネント用に設計されています。 通知イベントを生成する前に処理する行の数を示します。 プロパティは NotifyAfter 、一括コピー操作が進行中であっても、いつでも設定できます。 一括コピー操作中に行われた変更は、次の通知の後に有効になります。 新しい設定は、同じインスタンスに対する後続のすべての操作に適用されます。

が 0 未満の数値に設定されている場合 NotifyAfter は、 ArgumentOutOfRangeException がスローされます。

適用対象