Vorgehensweise: Überprüfen von Daten auf dem Abonnenten (RMO-Programmierung)

Die Replikation ermöglicht Ihnen mithilfe gespeicherter Replikationsprozeduren (RMO), programmgesteuert zu überprüfen, ob die Daten auf dem Abonnenten mit den Daten auf dem Verleger übereinstimmen. Welche Objekte Sie verwenden, hängt vom Typ der Replikationstopologie ab. Für die Transaktionsreplikation ist eine Überprüfung aller Abonnements für eine Veröffentlichung erforderlich.

So überprüfen Sie die Daten für alle Artikel in einer Transaktionsveröffentlichung

  1. Erstellen Sie eine Verbindung mit dem Verleger, indem Sie die ServerConnection-Klasse verwenden.

  2. Erstellen Sie eine Instanz der TransPublication-Klasse. Legen Sie die Name-Eigenschaft und die DatabaseName-Eigenschaft für die Veröffentlichung fest. Legen Sie die ConnectionContext-Eigenschaft auf die in Schritt 1 erstellte Verbindung fest.

  3. Rufen Sie die LoadProperties-Methode auf, um die restlichen Objekteigenschaften abzurufen. Wenn diese Methode false zurückgibt, sind die Veröffentlichungseigenschaften in Schritt 2 falsch definiert, oder die Veröffentlichung ist nicht vorhanden.

  4. Rufen Sie die ValidatePublication-Methode auf. Übergeben Sie die folgenden Werte:

    Dadurch werden die Artikel zur Überprüfung gekennzeichnet.

  5. Starten Sie den Verteilungs-Agent, falls er noch nicht ausgeführt wird, um alle Abonnements zu synchronisieren. Weitere Informationen finden Sie unter Vorgehensweise: Synchronisieren eines Pushabonnements (RMO-Programmierung) oder Vorgehensweise: Synchronisieren eines Pullabonnements (RMO-Programmierung). Das Ergebnis der Überprüfung wird in den Agentverlauf geschrieben. Weitere Informationen finden Sie unter Vorgehensweise: Programmgesteuertes Überwachen der Replikation (RMO-Programmierung).

So überprüfen Sie die Daten in allen Abonnements für eine Mergeveröffentlichung

  1. Erstellen Sie eine Verbindung mit dem Verleger, indem Sie die ServerConnection-Klasse verwenden.

  2. Erstellen Sie eine Instanz der MergePublication-Klasse. Legen Sie die Name-Eigenschaft und die DatabaseName-Eigenschaft für die Veröffentlichung fest. Legen Sie die ConnectionContext-Eigenschaft auf die in Schritt 1 erstellte Verbindung fest.

  3. Rufen Sie die LoadProperties-Methode auf, um die restlichen Objekteigenschaften abzurufen. Wenn diese Methode false zurückgibt, sind die Veröffentlichungseigenschaften in Schritt 2 falsch definiert, oder die Veröffentlichung ist nicht vorhanden.

  4. Rufen Sie die ValidatePublication-Methode auf. Übergeben Sie die gewünschte ValidationOption.

  5. Führen Sie für jedes Abonnement den Merge-Agent aus, um die Überprüfung zu starten, oder warten Sie die nächste geplante Ausführung des Agents ab. Weitere Informationen finden Sie unter Vorgehensweise: Synchronisieren eines Pullabonnements (RMO-Programmierung) und Vorgehensweise: Synchronisieren eines Pushabonnements (RMO-Programmierung). Das Ergebnis der Überprüfung wird in den Agentverlauf geschrieben. Diesen können Sie mithilfe des Replikationsmonitors anzeigen. Weitere Informationen finden Sie unter Vorgehensweise: Programmgesteuertes Überwachen der Replikation (RMO-Programmierung).

So überprüfen Sie die Daten in einem einzelnen Abonnement für eine Mergeveröffentlichung

  1. Erstellen Sie eine Verbindung mit dem Verleger, indem Sie die ServerConnection-Klasse verwenden.

  2. Erstellen Sie eine Instanz der MergePublication-Klasse. Legen Sie die Name-Eigenschaft und die DatabaseName-Eigenschaft für die Veröffentlichung fest. Legen Sie die ConnectionContext-Eigenschaft auf die in Schritt 1 erstellte Verbindung fest.

  3. Rufen Sie die LoadProperties-Methode auf, um die restlichen Objekteigenschaften abzurufen. Wenn diese Methode false zurückgibt, sind die Veröffentlichungseigenschaften in Schritt 2 falsch definiert, oder die Veröffentlichung ist nicht vorhanden.

  4. Rufen Sie die ValidateSubscription-Methode auf. Übergeben Sie den Namen des Abonnenten und der Abonnementdatenbank, der bzw. die überprüft wird, und die gewünschte ValidationOption.

  5. Führen Sie für das Abonnement den Merge-Agent aus, um die Überprüfung zu starten, oder warten Sie die nächste geplante Ausführung des Agents ab. Weitere Informationen finden Sie unter Vorgehensweise: Synchronisieren eines Pullabonnements (RMO-Programmierung) und Vorgehensweise: Synchronisieren eines Pushabonnements (RMO-Programmierung). Das Ergebnis der Überprüfung wird in den Agentverlauf geschrieben. Diesen können Sie mithilfe des Replikationsmonitors anzeigen. Weitere Informationen finden Sie unter Vorgehensweise: Programmgesteuertes Überwachen der Replikation (RMO-Programmierung).

Beispiel

In diesem Beispiel werden alle Abonnements für eine Transaktionsveröffentlichung für die Zeilenanzahlüberprüfung gekennzeichnet.

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

            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 = "AdventureWorks2008R2"

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

In diesem Beispiel wird ein bestimmtes Abonnement für eine Mergeveröffentlichung für die Zeilenanzahlüberprüfung gekennzeichnet.

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

            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 = "AdventureWorks2008R2"
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2008R2Replica"

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