共用方式為


如何:驗證訂閱者端的資料 (RMO 程式設計)

複寫能讓您以程式設計的方式,使用「複寫管理物件」(RMO) 驗證「訂閱者」和「發行者」兩端的資料相符。您使用的物件依照複寫拓樸的類型而定。交易式複寫需要驗證發行集的所有訂閱。

若要驗證交易式發行集中所有發行項的資料

  1. 使用 ServerConnection 類別建立與「發行者」的連接。

  2. 建立 TransPublication 類別的執行個體。設定發行集的 NameDatabaseName 屬性。將 ConnectionContext 屬性設定為在步驟 1 中建立的連接。

  3. 呼叫 LoadProperties 方法以取得物件的剩餘屬性。如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。

  4. 呼叫 ValidatePublication 方法。傳遞下列項目:

    這會標示要驗證的發行項。

  5. 如果尚未執行,請啟動「散發代理程式」以同步處理每個訂閱。如需詳細資訊,請參閱<如何:同步處理發送訂閱 (RMO 程式設計)>或<如何:同步處理提取訂閱 (RMO 程式設計)>。驗證作業的結果會寫入至代理程式記錄。如需詳細資訊,請參閱<如何:以程式設計方式監視複寫 (RMO 程式設計)>。

若要驗證所有合併發行集訂閱中的資料

  1. 使用 ServerConnection 類別建立與「發行者」的連接。

  2. 建立 MergePublication 類別的執行個體。設定發行集的 NameDatabaseName 屬性。將 ConnectionContext 屬性設定為在步驟 1 中建立的連接。

  3. 呼叫 LoadProperties 方法以取得物件的剩餘屬性。如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。

  4. 呼叫 ValidatePublication 方法。傳遞所要的 ValidationOption

  5. 為每個訂閱執行「合併代理程式」以啟動驗證,或等候下一個排程的代理程式執行。如需詳細資訊,請參閱<如何:同步處理提取訂閱 (RMO 程式設計)>和<如何:同步處理發送訂閱 (RMO 程式設計)>。驗證作業的結果會寫入至代理程式記錄,您可使用「複寫監視器」來加以檢視。如需詳細資訊,請參閱<如何:以程式設計方式監視複寫 (RMO 程式設計)>。

若要驗證合併發行集單一訂閱中的資料

  1. 使用 ServerConnection 類別建立與「發行者」的連接。

  2. 建立 MergePublication 類別的執行個體。設定發行集的 NameDatabaseName 屬性。將 ConnectionContext 屬性設定為在步驟 1 中建立的連接。

  3. 呼叫 LoadProperties 方法以取得物件的剩餘屬性。如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。

  4. 呼叫 ValidateSubscription 方法。傳遞正在進行驗證之「訂閱者」和訂閱資料庫的名稱以及所要的 ValidationOption

  5. 為訂閱執行「合併代理程式」以啟動驗證,或等候下一個排程的代理程式執行。如需詳細資訊,請參閱<如何:同步處理提取訂閱 (RMO 程式設計)>和<如何:同步處理發送訂閱 (RMO 程式設計)>。驗證作業的結果會寫入至代理程式記錄,您可使用「複寫監視器」來加以檢視。如需詳細資訊,請參閱<如何:以程式設計方式監視複寫 (RMO 程式設計)>。

範例

此範例會標示交易式發行集的所有訂閱,以進行列數驗證。

            // Define the server, database, and publication names
            string publisherName = publisherInstance;
            string publicationName = "AdvWorksProductTran";
            string publicationDbName = "AdventureWorks";

            TransPublication publication;

            // Create a connection to the Publisher.
            ServerConnection conn = new ServerConnection(publisherName);

            try
            {
                // Connect to the Publisher.
                conn.Connect();

                // Set the required properties for the publication.
                publication = new TransPublication();
                publication.ConnectionContext = conn;
                publication.Name = publicationName;
                publication.DatabaseName = publicationDbName;

                // If we can't get the properties for this publication, 
                // throw an application exception.
                if (publication.LoadProperties())
                {
                    // Initiate validataion for all subscriptions to this publication.
                    publication.ValidatePublication(ValidationOption.RowCountOnly,
                        ValidationMethod.ConditionalFast, false);

                    // If not already running, start the Distribution Agent at each 
                    // Subscriber to synchronize and validate the subscriptions.
                }
                else
                {
                    throw new ApplicationException(String.Format(
                        "Settings could not be retrieved for the publication. " +
                        "Ensure that the publication {0} exists on {1}.",
                        publicationName, publisherName));
                }
            }
            catch (Exception ex)
            {
                // Do error handling here.
                throw new ApplicationException(
                    "Subscription validation could not be initiated.", ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Define the server, database, and publication names
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks"

Dim publication As TransPublication

' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Set the required properties for the publication.
    publication = New TransPublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' If we can't get the properties for this publication, 
    ' throw an application exception.
    If publication.LoadProperties() Then

        ' Initiate validataion for all subscriptions to this publication.
        publication.ValidatePublication(ValidationOption.RowCountOnly, _
         ValidationMethod.ConditionalFast, False)

        ' If not already running, start the Distribution Agent at each 
        ' Subscriber to synchronize and validate the subscriptions.
    Else
        Throw New ApplicationException(String.Format( _
         "Settings could not be retrieved for the publication. " + _
         "Ensure that the publication {0} exists on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Do error handling here.
    Throw New ApplicationException( _
     "Subscription validation could not be initiated.", ex)
Finally
    conn.Disconnect()
End Try

此範例會標示合併式發行集的特定訂閱,以進行列數驗證。

          // Define the server, database, and publication names
            string publisherName = publisherInstance;
            string publicationName = "AdvWorksSalesOrdersMerge";
            string publicationDbName = "AdventureWorks";
            string subscriberName = subscriberInstance;
            string subscriptionDbName = "AdventureWorksReplica";

            MergePublication publication;

            // Create a connection to the Publisher.
            ServerConnection conn = new ServerConnection(publisherName);

            try
            {
                // Connect to the Publisher.
                conn.Connect();

                // Set the required properties for the publication.
                publication = new MergePublication();
                publication.ConnectionContext = conn;
                publication.Name = publicationName;
                publication.DatabaseName = publicationDbName;


                // If we can't get the properties for this merge publication, then throw an application exception.
                if (publication.LoadProperties())
                {
                    // Initiate validation of the specified subscription.
                    publication.ValidateSubscription(subscriberName,
                        subscriptionDbName, ValidationOption.RowCountOnly);
                    
                    // Start the Merge Agent to synchronize and validate the subscription.
                }
                else
                {
                    throw new ApplicationException(String.Format(
                        "Settings could not be retrieved for the publication. " +
                        "Ensure that the publication {0} exists on {1}.",
                        publicationName, publisherName));
                }
            }
            catch (Exception ex)
            {
                // Do error handling here.
                throw new ApplicationException(String.Format(
                    "The subscription at {0} to the {1} publication could not " +
                    "be validated.", subscriberName, publicationName), ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Define the server, database, and publication names
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks"
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorksReplica"

Dim publication As MergePublication

' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Set the required properties for the publication.
    publication = New MergePublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' If we can't get the properties for this merge publication, then throw an application exception.
    If publication.LoadProperties() Then
        ' Initiate validation of the specified subscription.
        publication.ValidateSubscription(subscriberName, _
         subscriptionDbName, ValidationOption.RowCountOnly)

        ' Start the Merge Agent to synchronize and validate the subscription.
    Else
        Throw New ApplicationException(String.Format( _
         "Settings could not be retrieved for the publication. " + _
         "Ensure that the publication {0} exists on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Do error handling here.
    Throw New ApplicationException(String.Format( _
     "The subscription at {0} to the {1} publication could not " + _
     "be validated.", subscriberName, publicationName), ex)
Finally
    conn.Disconnect()
End Try