Delen via


Een push-abonnement maken

Van toepassing op:SQL ServerAzure SQL Database

In dit onderwerp wordt beschreven hoe u een push-abonnement maakt in SQL Server met behulp van SQL Server Management Studio, Transact-SQL of Replicatiebeheerobjecten (RMO). Zie Een abonnement maken voor een niet-SQL Server-abonnee voor meer informatie over het maken van een push-abonnement voor een niet-SQL Server-abonnee.

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

Maak een push-abonnement bij de Publisher of de Subscriber met behulp van de wizard Nieuw Abonnement. Volg de pagina's in de wizard om:

  • Geef de uitgever en publicatie op.

  • Selecteer waar replicatieagents draaien. Voor een pushabonnement selecteert u Alle agents uitvoeren bij de distributeur (pushabonnementen) op de pagina Locatie van distributieagent of de pagina Agentlocatie samenvoegen , afhankelijk van het type publicatie.

  • Geef abonnees en abonnementsdatabases op.

  • Geef de aanmeldingen en wachtwoorden op die worden gebruikt voor verbindingen die zijn gemaakt door replicatieagents:

    • Specificeer op de pagina Beveiliging van distributieagenten de referentiegegevens voor abonnementen op momentopnamen en transactionele publicaties.

    • Als u abonnementen wilt samenvoegen, geeft u referenties op de pagina Beveiliging van de samenvoegagent op.

      Zie het beveiligingsmodel van de replicatieagent voor informatie over de machtigingen die elke agent nodig heeft.

  • Geef een synchronisatieschema op en wanneer de abonnee moet worden geïnitialiseerd.

  • Geef aanvullende opties op voor het samenvoegen van publicaties: abonnementstype en -waarden voor geparameteriseerde filters.

  • Geef aanvullende opties op voor transactionele publicaties die het bijwerken van abonnementen toestaan. Een optie is om te bepalen of abonnees wijzigingen direct moeten doorvoeren bij de Uitgever of ze naar een wachtrij moeten schrijven. Een andere optie is het instellen van authenticatiegegevens die worden gebruikt om verbinding te maken van de abonnee naar de uitgever.

  • Voer desgewenst een script uit voor het abonnement.

Een push-abonnement maken vanuit Publisher

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

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

  3. Klik met de rechtermuisknop op de publicatie waarvoor u een of meer abonnementen wilt maken en selecteer vervolgens Nieuwe abonnementen.

  4. Voltooi de pagina's in de wizard Nieuw abonnement.

Een push-abonnement aanmaken vanuit de abonnee

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

  2. Vouw de map Replicatie uit.

  3. Klik met de rechtermuisknop op de map Lokale abonnementen en selecteer vervolgens Nieuwe abonnementen.

  4. Op de pagina Publicatie van de wizard Nieuw abonnement, selecteer <SQL Server Publisher> zoeken of <Oracle Publisher> zoeken uit de vervolgkeuzelijst Publisher.

  5. Maak verbinding met publisher in het dialoogvenster Verbinding maken met server .

  6. Selecteer een publicatie op de pagina Publicatie .

  7. Voltooi de pagina's in de Wizard voor Nieuw Abonnement.

Transact-SQL gebruiken

U kunt pushabonnementen programmatisch maken met behulp van opgeslagen replicatieprocedures. De gebruikte opgeslagen procedures zijn afhankelijk van het type publicatie waartoe het abonnement behoort.

Belangrijk

Indien mogelijk vraagt u gebruikers om beveiligingsreferenties in te voeren tijdens runtime. Als u referenties in een scriptbestand moet opslaan, moet u het bestand beveiligen om onbevoegde toegang te voorkomen.

Een pushabonnement maken op een momentopname of transactionele publicatie

  1. Controleer in Publisher in de publicatiedatabase of de publicatie ondersteuning biedt voor pushabonnementen door sp_helppublication uit te voeren.

    • Als de waarde van allow_push1 is, worden pushabonnementen ondersteund.

    • Als de waarde van allow_push0 is, voert sp_changepublication uit. Geef allow_push op voor @property en true voor @value.

  2. Bij de Publisher in de publicatiedatabase, voer sp_addsubscription uit. Geef @publication, @subscriber en @destination_db op. Geef een pushwaarde op voor@subscription_type. Zie Een updatable abonnement maken voor een transactionele publicatie voor informatie over het bijwerken van abonnementen.

  3. Voer in Publisher in de publicatiedatabase sp_addpushsubscription_agent uit. Geef het volgende op:

    • De @subscriber, @subscriber_db, en @publication parameters.

    • De Microsoft Windows-gegevens waaronder de distributie-agent bij de distributeur draait voor @job_login en @job_password.

      Opmerking

      Verbindingen die zijn gemaakt via geïntegreerde Windows-verificatie, gebruiken altijd de Windows-referenties die zijn opgegeven door @job_login en @job_password. De distributieagent maakt altijd de lokale verbinding met de distributeur met behulp van geïntegreerde Windows-verificatie. De agent maakt standaard verbinding met de abonnee met behulp van geïntegreerde Windows-verificatie.

    • (Optioneel) Een waarde van 0 voor @subscriber_security_mode en de aanmeldingsgegevens van Microsoft SQL Server voor @subscriber_login en @subscriber_password. Geef deze parameters op als u SQL Server-verificatie moet gebruiken bij het maken van verbinding met de abonnee.

    • Een planning voor de distributieagenttaak voor dit abonnement. Zie Synchronisatieschema's opgeven voor meer informatie.

Belangrijk

Wanneer u een push-abonnement maakt bij een uitgever met een externe distributeur, worden de waarden die worden opgegeven voor alle parameters, inclusief job_login en job_password, verzonden naar de Distributeur als tekst zonder opmaak. Versleutel de verbinding tussen de uitgever en de externe distributeur voordat u deze opgeslagen procedure uitvoert. Zie Versleutelde verbindingen met de database-engine inschakelen (SQL Server Configuration Manager) voor meer informatie.

Een push-abonnement maken voor een samenvoegpublicatie

  1. Controleer bij Publisher in de publicatiedatabase of de publicatie ondersteuning biedt voor pushabonnementen door sp_helpmergepublication uit te voeren.

    • Als de waarde van allow_push1 is, ondersteunt de publicatie pushabonnementen.

    • Als de waarde van allow_push niet 1 is, voert u sp_changemergepublication uit. Geef allow_push op voor @property en true voor @value.

  2. Voer in Publisher in de publicatiedatabase sp_addmergesubscription uit. Geef de volgende parameters op:

    • @publication. Dit is de naam van de publicatie.

    • @subscriber_type. Geef voor een clientabonnement lokaal op. Voor een serverabonnement geeft u globaal op.

    • @subscription_priority. Geef voor een serverabonnement een prioriteit op voor het abonnement (0,00 tot 99,99).

    Zie geavanceerde detectie en oplossing van replicatieconflicten voor geavanceerde samenvoeging voor meer informatie.

  3. Voer in Publisher in de publicatiedatabase sp_addmergepushsubscription_agent uit. Geef het volgende op:

    • De @subscriber, @subscriber_db, en @publication parameters.

    • De Windows-referenties waaronder de samenvoegagent bij de distributeur wordt uitgevoerd voor @job_login en @job_password.

      Opmerking

      Verbindingen die zijn gemaakt via geïntegreerde Windows-verificatie, gebruiken altijd de Windows-referenties die zijn opgegeven door @job_login en @job_password. De Merge Agent maakt altijd de lokale verbinding met de distributeur met behulp van geïntegreerde Windows-verificatie. De agent maakt standaard verbinding met de abonnee met behulp van geïntegreerde Windows-verificatie.

    • (Optioneel) Een waarde van 0 voor @subscriber_security_mode en de sql Server-aanmeldingsgegevens voor @subscriber_login en @subscriber_password. Geef deze parameters op als u SQL Server-verificatie moet gebruiken bij het maken van verbinding met de abonnee.

    • (Optioneel) Een waarde van 0 voor @publisher_security_mode en de SQL Server-aanmeldingsgegevens voor @publisher_login en @publisher_password. Geef deze waarden op als u SQL Server-verificatie moet gebruiken bij het maken van verbinding met publisher.

    • Een planning voor de samenvoegagent-taak voor dit abonnement. Zie Synchronisatieschema's opgeven voor meer informatie.

Belangrijk

Wanneer u een push-abonnement maakt bij een uitgever met een externe distributeur, worden de waarden die worden opgegeven voor alle parameters, inclusief job_login en job_password, verzonden naar de Distributeur als tekst zonder opmaak. Versleutel de verbinding tussen de uitgever en de externe distributeur voordat u deze opgeslagen procedure uitvoert. Zie Versleutelde verbindingen met de database-engine inschakelen (SQL Server Configuration Manager) voor meer informatie.

Voorbeelden (Transact-SQL)

In het volgende voorbeeld wordt een push-abonnement gemaakt naar een transactionele publicatie. Aanmeldings- en wachtwoordwaarden worden tijdens runtime opgegeven via sqlcmd-scriptvariabelen .

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorks2022Replica';

--Add a push subscription to a transactional publication.
USE [AdventureWorks2022]
EXEC sp_addsubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @destination_db = @subscriptionDB, 
  @subscription_type = N'push';

--Add an agent job to synchronize the push subscription.
EXEC sp_addpushsubscription_agent 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @job_login = $(Login), 
  @job_password = $(Password);
GO

In het volgende voorbeeld wordt een push-abonnement gemaakt voor een samenvoegpublicatie. Aanmeldings- en wachtwoordwaarden worden tijdens runtime opgegeven via sqlcmd-scriptvariabelen .

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @hostname AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorksReplica'; 
SET @hostname = N'adventure-works\david8'

-- Add a push subscription to a merge publication.
USE [AdventureWorks2022];
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'push',
  @hostname = @hostname;

--Add an agent job to synchronize the push subscription.
EXEC sp_addmergepushsubscription_agent 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @job_login = $(Login), 
  @job_password = $(Password);
GO

Replicatiebeheerobjecten gebruiken

U kunt programmatisch pushabonnementen maken met behulp van RMO (Replication Management Objects). De RMO-klassen die u gebruikt om een push-abonnement te maken, zijn afhankelijk van het type publicatie waarnaar het abonnement wordt gemaakt.

Belangrijk

Indien mogelijk vraagt u gebruikers om beveiligingsreferenties in te voeren tijdens runtime. Als u referenties moet opslaan, gebruikt u de cryptografische services die microsoft Windows .NET Framework biedt.

Een pushabonnement maken op een momentopname of transactionele publicatie

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

  2. Maak een exemplaar van de TransPublication klasse met behulp van de Publisher-verbinding uit stap 1. Geef Name, DatabaseNameen ConnectionContext.

  3. Roep de LoadProperties methode aan. Als deze methode onwaar retourneert, zijn de eigenschappen die zijn opgegeven in stap 2 onjuist of bestaat de publicatie niet op de server.

  4. Voer een bitsgewijze logische AND (& in Visual C# en En in Visual Basic) uit tussen de Attributes eigenschap en AllowPush. Als het resultaat None is, stelt u Attributes in op het resultaat van een bitwise OR (| in Visual C# en Of in Visual Basic) tussen Attributes en AllowPush. CommitPropertyChanges Roep vervolgens aan om pushabonnementen in te schakelen.

  5. Als de abonnementsdatabase niet bestaat, maakt u deze met behulp van de Database klasse. Zie Databases maken, wijzigen en verwijderen voor meer informatie.

  6. Maak een exemplaar van de TransSubscription klasse.

  7. Stel de volgende abonnementseigenschappen in:

  8. Roep de Create methode aan.

Belangrijk

Wanneer u een push-abonnement maakt bij een uitgever met een externe distributeur, worden de waarden die worden opgegeven voor alle eigenschappen, inclusief SynchronizationAgentProcessSecurity, als tekst zonder opmaak naar de distributeur verzonden. Versleutel de verbinding tussen publisher en de externe distributeur voordat u de Create methode aanroept. Zie Versleutelde verbindingen met de database-engine inschakelen (SQL Server Configuration Manager) voor meer informatie.

Een push-abonnement maken voor een samenvoegpublicatie

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

  2. Maak een exemplaar van de MergePublication klasse met behulp van de Publisher-verbinding uit stap 1. Geef Name, DatabaseNameen ConnectionContext.

  3. Roep de LoadProperties methode aan. Als deze methode onwaar retourneert, zijn de eigenschappen die zijn opgegeven in stap 2 onjuist of bestaat de publicatie niet op de server.

  4. Voer een bitsgewijze logische AND (& in Visual C# en En in Visual Basic) uit tussen de Attributes eigenschap en AllowPush. Als het resultaat None is, stel Attributes in op het resultaat van een bitwise logische OF (| in Visual C# en Of in Visual Basic) tussen Attributes en AllowPush. CommitPropertyChanges Roep vervolgens aan om pushabonnementen in te schakelen.

  5. Als de abonnementsdatabase niet bestaat, maakt u deze met behulp van de Database klasse. Zie Databases maken, wijzigen en verwijderen voor meer informatie.

  6. Maak een exemplaar van de MergeSubscription klasse.

  7. Stel de volgende abonnementseigenschappen in:

  8. Roep de Create methode aan.

Belangrijk

Wanneer u een push-abonnement maakt bij een uitgever met een externe distributeur, worden de waarden die worden opgegeven voor alle eigenschappen, inclusief SynchronizationAgentProcessSecurity, als tekst zonder opmaak naar de distributeur verzonden. Versleutel de verbinding tussen publisher en de externe distributeur voordat u de Create methode aanroept. Zie Versleutelde verbindingen met de database-engine inschakelen (SQL Server Configuration Manager) voor meer informatie.

Voorbeelden (RMO)

In dit voorbeeld wordt een nieuw pushabonnement gemaakt op een transactionele publicatie. De Windows-accountreferenties die u gebruikt om de distributieagenttaak uit te voeren, worden tijdens runtime doorgegeven.

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

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

           // Create the objects that we need.
           TransPublication publication;
           TransSubscription subscription;

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

               // Ensure that the publication exists and that 
               // it supports push subscriptions.
               publication = new TransPublication();
               publication.Name = publicationName;
               publication.DatabaseName = publicationDbName;
               publication.ConnectionContext = conn;

               if (publication.IsExistingObject)
               {
                   if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
                   {
                       publication.Attributes |= PublicationAttributes.AllowPush;
                   }

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

                   // Specify the Windows login credentials for the Distribution Agent job.
                   subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
                   subscription.SynchronizationAgentProcessSecurity.Password = winPassword;

                   // By default, subscriptions to transactional publications are synchronized 
                   // continuously, but in this case we only want to synchronize on demand.
                   subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.OnDemand;

                   // Create the push subscription.
                   subscription.Create();
               }
               else
               {
                   // Do something here if the publication does not exist.
                   throw new ApplicationException(String.Format(
                       "The publication '{0}' does not exist on {1}.",
                       publicationName, publisherName));
               }
           }
           catch (Exception ex)
           {
               // Implement the appropriate error handling here.
               throw new ApplicationException(String.Format(
                   "The subscription to {0} could not be created.", publicationName), ex);
           }
           finally
           {
               conn.Disconnect();
           }
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksProductTran"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

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

' Create the objects that we need.
Dim publication As TransPublication
Dim subscription As TransSubscription

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

    ' Ensure that the publication exists and that 
    ' it supports push subscriptions.
    publication = New TransPublication()
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName
    publication.ConnectionContext = conn

    If publication.IsExistingObject Then
        If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
            publication.Attributes = publication.Attributes _
            Or PublicationAttributes.AllowPush
        End If

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

        ' Specify the Windows login credentials for the Distribution Agent job.
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword

        ' By default, subscriptions to transactional publications are synchronized 
        ' continuously, but in this case we only want to synchronize on demand.
        subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.OnDemand

        ' Create the push subscription.
        subscription.Create()
    Else
        ' Do something here if the publication does not exist.
        Throw New ApplicationException(String.Format( _
         "The publication '{0}' does not exist on {1}.", _
         publicationName, publisherName))
    End If

Catch ex As Exception
    ' Implement the appropriate error handling here.
    Throw New ApplicationException(String.Format( _
        "The subscription to {0} could not be created.", publicationName), ex)
Finally
    conn.Disconnect()
End Try

In dit voorbeeld wordt een nieuw pushabonnement gemaakt voor een samenvoegpublicatie. De Windows-accountreferenties die u gebruikt om de samenvoegagenttaak uit te voeren, worden tijdens runtime doorgegeven.

// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksSalesOrdersMerge";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";
string hostname = @"adventure-works\garrett1";

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

// Create the objects that we need.
MergePublication publication;
MergeSubscription subscription;

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

    // Ensure that the publication exists and that 
    // it supports push subscriptions.
    publication = new MergePublication();
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;
    publication.ConnectionContext = conn;

    if (publication.IsExistingObject)
    {
        if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
        {
            publication.Attributes |= PublicationAttributes.AllowPush;
        }

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

        // Set a schedule to synchronize the subscription every 2 hours
        // during weekdays from 6am to 10pm.
        subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly;
        subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32(0x003E);
        subscription.AgentSchedule.FrequencyRecurrenceFactor = 1;
        subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour;
        subscription.AgentSchedule.FrequencySubDayInterval = 2;
        subscription.AgentSchedule.ActiveStartDate = 20051108;
        subscription.AgentSchedule.ActiveEndDate = 20071231;
        subscription.AgentSchedule.ActiveStartTime = 060000;
        subscription.AgentSchedule.ActiveEndTime = 100000;

        // Specify the Windows login credentials for the Merge Agent job.
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword;

        // Create the push subscription.
        subscription.Create();
    }
    else
    {
        // Do something here if the publication does not exist.
        throw new ApplicationException(String.Format(
            "The publication '{0}' does not exist on {1}.",
            publicationName, publisherName));
    }
}
catch (Exception ex)
{
    // Implement the appropriate error handling here.
    throw new ApplicationException(String.Format(
        "The subscription to {0} could not be created.", publicationName), ex);
}
finally
{
    conn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"
Dim hostname As String = "adventure-works\garrett1"

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

' Create the objects that we need.
Dim publication As MergePublication
Dim subscription As MergeSubscription

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

    ' Ensure that the publication exists and that 
    ' it supports push subscriptions.
    publication = New MergePublication()
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName
    publication.ConnectionContext = conn

    If publication.IsExistingObject Then
        If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
            publication.Attributes = publication.Attributes _
            Or PublicationAttributes.AllowPush
        End If

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

        ' Set a schedule to synchronize the subscription every 2 hours
        ' during weekdays from 6am to 10pm.
        subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly
        subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32("0x003E", 16)
        subscription.AgentSchedule.FrequencyRecurrenceFactor = 1
        subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour
        subscription.AgentSchedule.FrequencySubDayInterval = 2
        subscription.AgentSchedule.ActiveStartDate = 20051108
        subscription.AgentSchedule.ActiveEndDate = 20071231
        subscription.AgentSchedule.ActiveStartTime = 60000
        subscription.AgentSchedule.ActiveEndTime = 100000

        ' Specify the Windows login credentials for the Merge Agent job.
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword

        ' Create the push subscription.
        subscription.Create()
    Else

        ' Do something here if the publication does not exist.
        Throw New ApplicationException(String.Format( _
         "The publication '{0}' does not exist on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here.
    Throw New ApplicationException(String.Format( _
    "The subscription to {0} could not be created.", publicationName), ex)
Finally
    conn.Disconnect()
End Try