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 屬性的整數值;如果尚未設定這個屬性,則為零。

範例

下列主控台應用程式示範如何使用已經開啟的連接大量載入資料。 屬性 NotifyAfter 會設定為在每 50 個數據列複製到資料表之後呼叫事件處理常式。

在此範例中,連線會先用來將資料從SQL Server資料表讀取到 SqlDataReader 實例。 然後會開啟第二個連線來大量複製資料。 請注意,來源資料不一定位於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屬性可以隨時設定,即使大量複製作業正在進行也一樣。 在大量複製作業期間所做的變更會在下一個通知之後生效。 新的設定會套用至相同實例上的所有後續作業。

如果 NotifyAfter 設定為小於零的數位, ArgumentOutOfRangeException 則會擲回 。

適用於