SqlBulkCopy.SqlRowsCopied 事件

定義

每次處理完 NotifyAfter 屬性指定的資料列數時就會發生。

public:
 event Microsoft::Data::SqlClient::SqlRowsCopiedEventHandler ^ SqlRowsCopied;
public event Microsoft.Data.SqlClient.SqlRowsCopiedEventHandler SqlRowsCopied;
member this.SqlRowsCopied : Microsoft.Data.SqlClient.SqlRowsCopiedEventHandler 
Public Custom Event SqlRowsCopied As SqlRowsCopiedEventHandler 
Public Event SqlRowsCopied As SqlRowsCopiedEventHandler 

事件類型

範例

下列主控台應用程式示範如何使用已經開啟的連接大量載入資料。 屬性 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;";
    }
}

備註

請注意, 和 BatchSizeNotifyAfter 設定是獨立的。 SqlRowsCopied收到事件並不表示任何資料列都已傳送至伺服器或已認可。

您無法從此事件呼叫 SqlBulkCopy.Close () Close() 或 SqlConnection.Close (Close()) 。 這樣做會導致 InvalidOperationException 擲回 ,而且 SqlBulkCopy 物件狀態不會變更。 如果使用者想要取消事件的作業, Abort 可以使用 的 SqlRowsCopiedEventArgs 屬性。 (如需使用 Abort property.) 的範例,請參閱交易和大量複製作業

在大量複製作業執行期間,連線不支援任何動作,而且建議您不要在事件期間 SqlRowsCopied 使用相同的連接。 不過,您可以開啟不同的連線。

適用於