Delen via


Een pushabonnement synchroniseren

Van toepassing op:SQL ServerAzure SQL Database

In dit onderwerp wordt beschreven hoe u een pushabonnement in SQL Server synchroniseert met behulp van SQL Server Management Studio, replicatieagents of RMO-objecten (Replication Management Objects).

Opmerking

Azure SQL Managed Instance kan een uitgever, distributeur en abonnee zijn voor momentopname en transactionele replicatie. Databases in Azure SQL Database kunnen alleen pushabonnees zijn voor momentopname en transactionele replicatie. Zie Transactionele replicatie met Azure SQL Database en Azure SQL Managed Instancevoor meer informatie.

SQL Server Management Studio gebruiken

Abonnementen worden gesynchroniseerd door de distributieagent (voor momentopname en transactionele replicatie) of de samenvoegagent (voor samenvoegreplicatie). Agents kunnen continu worden uitgevoerd, op aanvraag worden uitgevoerd of volgens een schema worden uitgevoerd. Zie Synchronisatieschema's opgeven voor meer informatie over het opgeven van synchronisatieschema's.

Synchroniseer een abonnement op aanvraag vanuit de mappen Lokale publicaties en lokale abonnementen in Microsoft SQL Server Management Studio en het tabblad Alle abonnementen in Replicatiecontrole. Abonnementen op Oracle-publicaties kunnen niet op aanvraag worden gesynchroniseerd vanaf de abonnee. Zie Replicatiecontrole starten voor meer informatie over het starten van replicatiecontrole.

Een pushabonnement op aanvraag synchroniseren in Management Studio (bij de Publisher)

  1. Maak verbinding met Publisher in Management Studio en vouw vervolgens het serverknooppunt uit.

  2. Vouw de map Replicatie uit en vouw vervolgens de map Lokale publicaties uit.

  3. Vouw de publicatie uit waarvoor u abonnementen wilt synchroniseren.

  4. Klik met de rechtermuisknop op het abonnement dat u wilt synchroniseren en klik vervolgens op Synchronisatiestatus weergeven.

  5. Klik in het dialoogvenster Synchronisatiestatus weergeven - <Subscriber>:<SubscriptionDatabase> op Start. Wanneer de synchronisatie is voltooid, wordt het bericht Synchronisatie voltooid weergegeven.

  6. Klik op sluiten.

Een pushabonnement op aanvraag synchroniseren in Management Studio (bij de abonnee)

  1. Maak verbinding met de abonnee in Management Studio en vouw vervolgens het serverknooppunt uit.

  2. Vouw de map Replicatie uit en vouw vervolgens de map Lokale abonnementen uit.

  3. Klik met de rechtermuisknop op het abonnement dat u wilt synchroniseren en klik vervolgens op Synchronisatiestatus weergeven.

  4. Er wordt een bericht weergegeven over het tot stand brengen van een verbinding met de distributeur. Klik op OK.

  5. Klik in het dialoogvenster Synchronisatiestatus weergeven - <Subscriber>:<SubscriptionDatabase> op Start. Wanneer de synchronisatie is voltooid, wordt het bericht Synchronisatie voltooid weergegeven.

  6. Klik op sluiten.

Een pushabonnement op aanvraag synchroniseren in Replication Monitor

  1. Vouw in Replicatiecontrole een Publisher-groep uit in het linkerdeelvenster, vouw een Uitgever uit en klik vervolgens op een publicatie.

  2. Klik op het tabblad Alle abonnementen .

  3. Klik met de rechtermuisknop op het abonnement dat u wilt synchroniseren en klik vervolgens op Synchroniseren starten.

  4. Als u de synchronisatievoortgang wilt weergeven, klikt u met de rechtermuisknop op het abonnement en klikt u vervolgens op Details weergeven.

Replicatieagents gebruiken

Pushabonnementen kunnen programmatisch en on-demand worden gesynchroniseerd door het juiste uitvoerbare bestand van de replicatieagent aan te roepen vanaf de opdrachtprompt. Het uitvoerbare bestand van de replicatieagent dat wordt aangeroepen, is afhankelijk van het type publicatie waartoe het pushabonnement behoort.

De distributieagent starten om een push-abonnement te synchroniseren met een transactionele publicatie

  1. Voerdistrib.exeuit vanaf de opdrachtprompt of in een batchbestand bij de distributeur. Geef de volgende opdrachtregelargumenten op:

    • -Uitgever

    • -PublisherDB

    • -Verdeler

    • -Abonnee

    • -SubscriberDB

    • -SubscriptionType = 0

    Als u SQL Server-verificatie gebruikt, moet u ook de volgende argumenten opgeven:

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -SubscriberSecurityMode = 0

      Belangrijk

      Gebruik waar mogelijk Windows-verificatie.

De samenvoegagent starten om een push-abonnement te synchroniseren met een samenvoegpublicatie

  1. Voerreplmerg.exeuit vanaf de opdrachtprompt of in een batchbestand bij de distributeur. Geef de volgende opdrachtregelargumenten op:

    • -Uitgever

    • -PublisherDB

    • -Publicatie

    • -Verdeler

    • -Abonnee

    • -SubscriberDB

    • -SubscriptionType = 0

    Als u SQL Server-verificatie gebruikt, moet u ook de volgende argumenten opgeven:

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -SubscriberSecurityMode = 0

      Belangrijk

      Gebruik waar mogelijk Windows-verificatie.

Voorbeelden (replicatieagenten)

In het volgende voorbeeld wordt de distributieagent gestart om een push-abonnement te synchroniseren.

  
REM -- Declare the variables.  
SET Publisher=%instancename%  
SET Subscriber=%instancename%  
SET PublicationDB=AdventureWorks2022  
SET SubscriptionDB=AdventureWorks2022Replica   
SET Publication=AdvWorksProductsTran  
  
REM -- Start the Distribution Agent with four subscription streams.  
REM -- The following command must be supplied without line breaks.  
"C:\Program Files\Microsoft SQL Server\120\COM\DISTRIB.EXE" -Subscriber %Subscriber%   
-SubscriberDB %SubscriptionDB% -SubscriberSecurityMode 1 -Publication %Publication%   
-Publisher %Publisher% -PublisherDB %PublicationDB% -Distributor %Publisher%   
-DistributorSecurityMode 1 -Continuous -SubscriptionType 0 -SubscriptionStreams 4  
  

In het volgende voorbeeld wordt de Merge Agent gestart om een push-abonnement te synchroniseren.

  
REM -- Declare the variables.  
SET Publisher=%instancename%  
SET Subscriber=%instancename%  
SET PublicationDB=AdventureWorks2022  
SET SubscriptionDB=AdventureWorks2022Replica   
SET Publication=AdvWorksSalesOrdersMerge  
  
REM -- Start the Merge Agent.  
REM -- The following command must be supplied without line breaks.  
"C:\Program Files\Microsoft SQL Server\120\COM\REPLMERG.EXE"  -Publisher %Publisher%   
-Subscriber  %Subscriber%  -Distributor %Publisher% -PublisherDB  %PublicationDB%   
-SubscriberDB %SubscriptionDB% -Publication %Publication% -PublisherSecurityMode 1   
-OutputVerboseLevel 3  -Output -SubscriberSecurityMode 1  -SubscriptionType 0   
-DistributorSecurityMode 1  
  

Replicatiebeheerobjecten (RMO) gebruiken

U kunt pushabonnementen programmatisch synchroniseren met behulp van RMO (Replication Management Objects) en beheerde codetoegang tot functies van de replicatieagent. De klassen die u gebruikt om een push-abonnement te synchroniseren, zijn afhankelijk van het type publicatie waartoe het abonnement behoort.

Opmerking

Als u een synchronisatie wilt starten die autonoom wordt uitgevoerd zonder dat dit van invloed is op uw toepassing, start u de agent asynchroon. Als u echter het resultaat van de synchronisatie wilt controleren en callbacks van de agent wilt ontvangen tijdens het synchronisatieproces (bijvoorbeeld als u een voortgangsbalk wilt weergeven), moet u de agent synchroon starten. Voor abonnees van Microsoft SQL Server 2005 Express-editie moet u de agent synchroon starten.

Een push-abonnement synchroniseren met een momentopname of transactionele publicatie

  1. Maak een verbinding met de distributeur met behulp van de ServerConnection klasse.

  2. Maak een exemplaar van de TransSubscription klasse en stel de volgende eigenschappen in:

  3. Roep de LoadProperties methode aan om de resterende abonnementseigenschappen op te halen. Als deze methode onwaar retourneert, controleert u of het abonnement bestaat.

  4. Start de distributieagent op een van de volgende manieren bij de distributeur:

    • Roep de SynchronizeWithJob-methode aan op de instantie van TransSubscription uit stap 2. Met deze methode wordt de distributieagent asynchroon gestart en wordt het besturingselement onmiddellijk naar uw toepassing geretourneerd terwijl de agenttaak wordt uitgevoerd. U kunt deze methode niet aanroepen als het abonnement is gemaakt met de waarde false voor CreateSyncAgentByDefault.

    • Haal een exemplaar van de TransSynchronizationAgent klasse op uit de SynchronizationAgent eigenschap en roep de methode aan Synchronize . Met deze methode wordt de agent synchroon gestart en blijft de controle bij het uitvoeren van de agenttaak. Tijdens de synchrone uitvoering kunt u het Status event afhandelen terwijl de agent actief is.

Een push-abonnement synchroniseren met een samenvoegpublicatie

  1. Maak een verbinding met de distributeur met behulp van de ServerConnection klasse.

  2. Maak een exemplaar van de MergeSubscription klasse en stel de volgende eigenschappen in:

  3. Roep de LoadProperties methode aan om de resterende abonnementseigenschappen op te halen. Als deze methode onwaar retourneert, controleert u of het abonnement bestaat.

  4. Start de samenvoegagent op een van de volgende manieren bij de distributeur:

    • Roep de SynchronizeWithJob-methode aan op de instantie van MergeSubscription uit stap 2. Met deze methode wordt de Merge Agent asynchroon gestart en krijgt uw toepassing onmiddellijk weer controle terwijl de agenttaak wordt uitgevoerd. U kunt deze methode niet aanroepen als het abonnement is gemaakt met de waarde false voor CreateSyncAgentByDefault.

    • Haal een exemplaar van de MergeSynchronizationAgent klasse op uit de SynchronizationAgent eigenschap en roep de methode aan Synchronize . Met deze methode wordt de samenvoegagent synchroon gestart en blijft het besturingselement bij de actieve agenttaak. Tijdens de synchrone uitvoering kunt u de Status gebeurtenis afhandelen terwijl de agent draait.

Voorbeelden (RMO)

In dit voorbeeld wordt een push-abonnement gesynchroniseerd met een transactionele publicatie, waarbij de agent asynchroon wordt gestart met behulp van de agenttaak.

// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";

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

TransSubscription subscription;

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

    // Instantiate the push subscription.
    subscription = new TransSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = publicationDbName;
    subscription.PublicationName = publicationName;
    subscription.SubscriptionDBName = subscriptionDbName;
    subscription.SubscriberName = subscriberName;

    // If the push subscription and the job exists, start the agent job.
    if (subscription.LoadProperties() && subscription.AgentJobId != null)
    {
        // Start the Distribution Agent asynchronously.
        subscription.SynchronizeWithJob();
    }
    else
    {
        // Do something here if the subscription does not exist.
        throw new ApplicationException(String.Format(
            "A subscription to '{0}' does not exists on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

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

Dim subscription As TransSubscription

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

    ' Instantiate the push subscription.
    subscription = New TransSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = publicationDbName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.SubscriberName = subscriberName

    ' If the push subscription and the job exists, start the agent job.
    If subscription.LoadProperties() And Not subscription.AgentJobId Is Nothing Then
        ' Start the Distribution Agent asynchronously.
        subscription.SynchronizeWithJob()
    Else
        ' Do something here if the subscription does not exist.
        Throw New ApplicationException(String.Format( _
         "A subscription to '{0}' does not exists on {1}", _
         publicationName, subscriberName))
    End If
Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try

In dit voorbeeld wordt een push-abonnement gesynchroniseerd met een transactionele publicatie, waarbij de agent synchroon wordt gestart.

// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";

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

TransSubscription subscription;

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

    // Define the push subscription.
    subscription = new TransSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = publicationDbName;
    subscription.PublicationName = publicationName;
    subscription.SubscriptionDBName = subscriptionDbName;
    subscription.SubscriberName = subscriberName;

    // If the push subscription exists, start the synchronization.
    if (subscription.LoadProperties())
    {
        // Check that we have enough metadata to start the agent.
        if (subscription.SubscriberSecurity != null)
        {
            // Synchronously start the Distribution Agent for the subscription.
            subscription.SynchronizationAgent.Synchronize();
        }
        else
        {
            throw new ApplicationException("There is insufficent metadata to " +
                "synchronize the subscription. Recreate the subscription with " +
                "the agent job or supply the required agent properties at run time.");
        }
    }
    else
    {
        // Do something here if the push subscription does not exist.
        throw new ApplicationException(String.Format(
            "The subscription to '{0}' does not exist on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

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

Dim subscription As TransSubscription

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

    ' Define the push subscription.
    subscription = New TransSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = publicationDbName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.SubscriberName = subscriberName

    ' If the push subscription exists, start the synchronization.
    If subscription.LoadProperties() Then
        ' Check that we have enough metadata to start the agent.
        If Not subscription.SubscriberSecurity Is Nothing Then

            ' Synchronously start the Distribution Agent for the subscription.
            subscription.SynchronizationAgent.Synchronize()
        Else
            Throw New ApplicationException("There is insufficent metadata to " + _
             "synchronize the subscription. Recreate the subscription with " + _
             "the agent job or supply the required agent properties at run time.")
        End If
    Else
        ' Do something here if the push subscription does not exist.
        Throw New ApplicationException(String.Format( _
         "The subscription to '{0}' does not exist on {1}", _
         publicationName, subscriberName))
    End If
Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try

In dit voorbeeld wordt een push-abonnement gesynchroniseerd met een samenvoegpublicatie, waarbij de agent asynchroon wordt gestart met behulp van de agent-taak.

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

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

MergeSubscription subscription;

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

    // Define push subscription.
    subscription = new MergeSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = publicationDbName;
    subscription.PublicationName = publicationName;
    subscription.SubscriptionDBName = subscriptionDbName;
    subscription.SubscriberName = subscriberName;

    // If the push subscription and the job exists, start the agent job.
    if (subscription.LoadProperties() && subscription.AgentJobId != null)
    {
        // Start the Merge Agent asynchronously.
        subscription.SynchronizeWithJob();
    }
    else
    {
        // Do something here if the subscription does not exist.
        throw new ApplicationException(String.Format(
            "A subscription to '{0}' does not exists on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

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

Dim subscription As MergeSubscription

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

    ' Define push subscription.
    subscription = New MergeSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = publicationDbName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.SubscriberName = subscriberName

    ' If the push subscription and the job exists, start the agent job.
    If subscription.LoadProperties() And Not subscription.AgentJobId Is Nothing Then
        ' Start the Merge Agent asynchronously.
        subscription.SynchronizeWithJob()
    Else
        ' Do something here if the subscription does not exist.
        Throw New ApplicationException(String.Format( _
            "A subscription to '{0}' does not exists on {1}", _
            publicationName, subscriberName))
    End If
Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try

In dit voorbeeld wordt een push-abonnement gesynchroniseerd met een samenvoegpublicatie, waarbij de agent synchroon wordt gestart.

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

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

MergeSubscription subscription;

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

    // Define the subscription.
    subscription = new MergeSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = publicationDbName;
    subscription.PublicationName = publicationName;
    subscription.SubscriptionDBName = subscriptionDbName;
    subscription.SubscriberName = subscriberName;

    // If the push subscription exists, start the synchronization.
    if (subscription.LoadProperties())
    {
        // Check that we have enough metadata to start the agent.
        if (subscription.SubscriberSecurity != null)
        {
            // Synchronously start the Merge Agent for the subscription.
            subscription.SynchronizationAgent.Synchronize();
        }
        else
        {
            throw new ApplicationException("There is insufficent metadata to " +
                "synchronize the subscription. Recreate the subscription with " +
                "the agent job or supply the required agent properties at run time.");
        }
    }
    else
    {
        // Do something here if the push subscription does not exist.
        throw new ApplicationException(String.Format(
            "The subscription to '{0}' does not exist on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

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

Dim subscription As MergeSubscription

Try
    ' Connect to the Publisher
    conn.Connect()

    ' Define the subscription.
    subscription = New MergeSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = publicationDbName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.SubscriberName = subscriberName

    ' If the push subscription exists, start the synchronization.
    If subscription.LoadProperties() Then
        ' Check that we have enough metadata to start the agent.
        If Not subscription.SubscriberSecurity Is Nothing Then
            ' Synchronously start the Merge Agent for the subscription.
            ' Log agent messages to an output file.
            subscription.SynchronizationAgent.Output = "mergeagent.log"
            subscription.SynchronizationAgent.OutputVerboseLevel = 2
            subscription.SynchronizationAgent.Synchronize()
        Else
            Throw New ApplicationException("There is insufficent metadata to " + _
             "synchronize the subscription. Recreate the subscription with " + _
             "the agent job or supply the required agent properties at run time.")
        End If
    Else
        ' Do something here if the push subscription does not exist.
        Throw New ApplicationException(String.Format( _
         "The subscription to '{0}' does not exist on {1}", _
         publicationName, subscriberName))
    End If
Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try