Lekéréses előfizetés létrehozása

A következőkre vonatkozik:SQL ServerAzure SQL Managed Instance

Ez a téma bemutatja, hogyan lehet pull előfizetést létrehozni az SQL Server-ben az SQL Server Management Studio, Transact-SQL vagy Replication Management Objects (RMO) használatával.

A P2P replikációhoz pull előfizetés beállítása script segítségével lehetséges, de a varázslón keresztül nem elérhető.

Az SQL Server Management Studio használata

Hozz létre pull előfizetést a Publisher-nél vagy az Előfizetőnél az Új Előfizetés Varázslóval. Kövesse a varázsló lapjait a következőhöz:

  • Adja meg a Közzétevőt és a kiadványt.

  • Válassza ki, hogy a replikációs ügynökök hol fognak futni. Pull előfizetéshez válassza ki a Run every agent at its Subscriber (pull subscriptions) opciót a Distribution Agent Location oldalon vagy a Merge Agent Location oldalon, a kiadvány típusától függően.

  • Adja meg az előfizetőket és az előfizetési adatbázisokat.

  • Adja meg a replikációs ügynökök által létrehozott kapcsolatokhoz használt bejelentkezéseket és jelszavakat:

    • A pillanatfelvételekre és tranzakciós kiadványokra való előfizetésekhez adja meg a hitelesítő adatokat a Terjesztési ügynök biztonsági beállításai oldalán.

    • A kiadványokat egyesítő előfizetésekhez adja meg a hitelesítő adatokat az Ügynök egyesítése biztonsági oldalon.

    Az egyes ügynökök jogosultságairól információért lásd: Replikációs ügynök biztonsági modell.

  • Adja meg a szinkronizálás ütemezését, és hogy mikor kell inicializálni az előfizetőt.

  • Határozz meg további opciókat a kiadványok összevonásához: előfizetési típus; paraméterezett szűréshez szükséges értékeket; valamint információt a HTTPS segítségével történő szinkronizáláshoz, ha a publikáció engedélyezett a webes szinkronizációra.

  • Határozz meg további opciókat tranzakciós kiadványokhoz, amelyek lehetővé teszik az előfizetések frissítését: hogy az előfizetőknek azonnal köteleződjenek a Publisher-nél a változtatások, vagy írjanak sorba; a jogosultságok, amelyeket az Előfizetőtől a Publisher-hez való kapcsoláshoz használnak.

  • Opcionálisan scripteld az előfizetést.

A Publisher által előkészítő előfizetés létrehozása

  1. Csatlakozzon a Publisherhez a Microsoft SQL Server Management Studióban, majd bontsa ki a kiszolgálócsomópontot.

  2. Bontsa ki a Replikáció mappát, majd bontsa ki a Helyi kiadványok mappát.

  3. Jobb kattints arra a kiadványra, amelyhez egy vagy több előfizetést szeretnél létrehozni, majd kattints az Új előfizetések gombra.

  4. Töltse ki a lapokat az Új előfizetés varázslóban.

Pull előfizetés létrehozása az előfizetőtől

  1. Csatlakozzon az előfizetőhöz az SQL Server Management Studióban, majd bontsa ki a kiszolgálócsomópontot.

  2. Nyissa meg a Replikáció mappát.

  3. Kattintson a jobb gombbal a Helyi előfizetések mappára, majd az Új előfizetések parancsra.

  4. Az Új előfizetés varázsló Közzététel lapján válassza <az SQL Server Publisher> keresése vagy <az Oracle Publisher> keresése lehetőséget a Publisher legördülő listából.

  5. Csatlakozás a Publisherhez a Csatlakozás a kiszolgálóhoz párbeszédpanelen.

  6. Válasszon ki egy kiadványt a Kiadvány lapon.

  7. Töltse ki a lapokat az Új előfizetés varázslóban.

Transact-SQL használata

A pull előfizetések programozott módon hozhatók létre replikációs tárolt eljárásokkal. A használt tárolt eljárások attól függenek, hogy milyen típusú kiadványhoz tartozik az előfizetés.

Pull előfizetés létrehozása egy pillanatképhez vagy tranzakciós kiadványhoz

  1. A Publisher után ellenőrizd, hogy a kiadvány támogatja a pull előfizetéseket sp_helppublication (Transact-SQL) futtatásával.

    • Ha az eredményhalmazban allow_pullértéke 1, akkor a kiadvány támogatja a pull előfizetéseket.

    • Ha allow_pull értéke 0, hajtsuk végre sp_changepublication (Transact-SQL)-et, megadva allow_pull @property-re,igaz @value-re.

  2. A Feliratkozó esetében hajtsd végre a sp_addpullsubscription (Transact-SQL) parancsot. Határozd meg @publisher és @publication. Az előfizetések frissítéséről további információért lásd: Frissíthető előfizetés létrehozása tranzakciós kiadványhoz.

  3. A Feliratkozó esetén hajtsd végre a sp_addpullsubscription_agent (Transact-SQL) parancsot. Adja meg a következőket:

    • A @publisher, @publisher_db és @publication paraméterek.

    • A Microsoft Windows hitelességek, amelyek alatt az Subscriber Distribution Agent @job_login és @job_password fut.

      Note

      Az Windows integrált hitelesítéssel létrejött kapcsolatok mindig a @job_login és @job_password által meghatározott Windows hitelesítést használják. A terjesztési ügynök mindig a Windows integrált hitelesítés használatával hozza létre a helyi kapcsolatot az előfizetővel. Alapértelmezés szerint az ügynök a Windows integrált hitelesítéssel csatlakozik a Forgalmazóhoz.

    • (Opcionális) A @distributor_security_modeértéke 0, @distributor_login és @distributor_password esetén pedig a SQL Server bejelentkezési adatok, ha SQL Server hitelesítést kell használni a Forgalmazóhoz való csatlakozáskor.

    • Az előfizetés terjesztési ügynök feladatának ütemezése. További információ: Szinkronizálási ütemezések megadása.

  4. A Publisher után hajtsd végre a sp_addsubscription (Transact-SQL) parancsot a pull előfizetés regisztrálásához. Adja meg @publication, @subscriber és @destination_db. Adja meg a lekérés értéket @subscription_type számára.

Pull előfizetés létrehozásához egy merge kiadványhoz

  1. A Publisher után ellenőrizd, hogy a kiadvány támogatja a pull előfizetéseket a sp_helpmergepublication (Transact-SQL) végrehajtásával.

    • Ha az eredményhalmazban allow_pullértéke 1, akkor a kiadvány támogatja a pull előfizetéseket.

    • Ha allow_pull értéke 0, hajtsuk végre sp_changemergepublication (Transact-SQL)-et, megadva allow_pull@property-re, és igaz @value-re.

  2. A Feliratkozó esetén hajtsd végre a sp_addmergepullsubscription (Transact-SQL) parancsot. Megadjuk a @publisher, @publisher_db, @publication és az alábbi paramétereket:

  3. A Feliratkozó esetében hajtsd végre sp_addmergepullsubscription_agent (Transact-SQL) parancsot. Adja meg a következő paramétereket:

    • @publisher, @publisher_db és @publication.

    • A Windows hitelességek, amelyek alatt az Előfizető Merge Agent @job_login és @job_password fut.

      Note

      Az Windows integrált hitelesítéssel létrejött kapcsolatok mindig a @job_login és @job_password által meghatározott Windows hitelesítést használják. A Merge Agent mindig helyi kapcsolatot teremt az Előfizetővel a Windows integrált hitelesítéssel. Alapértelmezés szerint az ügynök a Windows integrált hitelesítéssel csatlakozik a Forgalmazóhoz és a Publisher-hez.

    • (Opcionális) A @distributor_security_modeértéke 0, @distributor_login és @distributor_password esetén pedig a SQL Server bejelentkezési adatok, ha SQL Server hitelesítést kell használni a Forgalmazóhoz való csatlakozáskor.

    • (Opcionális) 0 érték az @publisher_security_mode esetén, a SQL Server bejelentkezési adatok pedig @publisher_login és @publisher_password esetén, ha SQL Server hitelesítést kell használni a Publisher-hez való csatlakozáskor.

    • Az ennek az előfizetésnek szóló Merge Agent feladat ütemezése. További információért lásd: Frissíthető előfizetés létrehozása tranzakciós kiadványhoz.

  4. A Publisher esetén hajtsd végre sp_addmergesubscription (Transact-SQL). Megadja @publication, @subscriber, @subscriber_db és a húzás értékét @subscription_type-hez. Ez regisztrálja a pull előfizetést.

Példák (Transact-SQL)

A következő példa egy pull előfizetést hoz létre egy tranzakciós kiadványhoz. Az első kötetet a Subscriber-nél hajtják végre, a második részt pedig a Publisher-nél. A bejelentkezési és jelszóértékek futásidőben jelennek meg sqlcmd szkriptelési változók használatával.

-- 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".  
  
-- Execute this batch at the Subscriber.  
DECLARE @publication AS sysname;  
DECLARE @publisher AS sysname;  
DECLARE @publicationDB AS sysname;  
SET @publication = N'AdvWorksProductTran';  
SET @publisher = $(PubServer);  
SET @publicationDB = N'AdventureWorks';  
  
-- At the subscription database, create a pull subscription   
-- to a transactional publication.  
USE [AdventureWorksReplica]  
EXEC sp_addpullsubscription   
  @publisher = @publisher,   
  @publication = @publication,   
  @publisher_db = @publicationDB;  
  
-- Add an agent job to synchronize the pull subscription.  
EXEC sp_addpullsubscription_agent   
  @publisher = @publisher,   
  @publisher_db = @publicationDB,   
  @publication = @publication,   
  @distributor = @publisher,   
  @job_login = $(Login),   
  @job_password = $(Password);  
GO  
-- 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".  
  
-- Execute this batch at the Publisher.  
DECLARE @publication AS sysname;  
DECLARE @subscriber AS sysname;  
DECLARE @subscriptionDB AS sysname;  
SET @publication = N'AdvWorksProductTran';  
SET @subscriber = $(SubServer);  
SET @subscriptionDB = N'AdventureWorksReplica';  
  
-- At the Publisher, register the subscription, using the defaults.  
EXEC sp_addsubscription   
  @publication = @publication,   
  @subscriber = @subscriber,   
  @destination_db = @subscriptionDB,   
  @subscription_type = N'pull',  
  @status = N'subscribed';  
GO  
  

A következő példa egy pull előfizetést hoz létre egy egyesítő kiadványhoz. Az első kötetet a Subscriber-nél hajtják végre, a második részt pedig a Publisher-nél. A bejelentkezési és jelszóértékeket futásidőben sqlcmd szkriptváltozókkal szolgáltatják.

-- 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".  
  
-- Execute this batch at the Subscriber.  
DECLARE @publication AS sysname;  
DECLARE @publisher AS sysname;  
DECLARE @publicationDB AS sysname;  
DECLARE @hostname AS sysname;  
SET @publication = N'AdvWorksSalesOrdersMerge';  
SET @publisher = $(PubServer);  
SET @publicationDB = N'AdventureWorks';  
SET @hostname = N'adventure-works\david8';  
  
-- At the subscription database, create a pull subscription   
-- to a merge publication.  
USE [AdventureWorksReplica]  
EXEC sp_addmergepullsubscription   
  @publisher = @publisher,   
  @publication = @publication,   
  @publisher_db = @publicationDB;  
  
-- Add an agent job to synchronize the pull subscription.   
EXEC sp_addmergepullsubscription_agent   
  @publisher = @publisher,   
  @publisher_db = @publicationDB,   
  @publication = @publication,   
  @distributor = @publisher,   
  @job_login = $(Login),   
  @job_password = $(Password),  
  @hostname = @hostname;  
GO  
-- Execute this batch at the Publisher.  
DECLARE @myMergePub  AS sysname;  
DECLARE @mySub       AS sysname;  
DECLARE @mySubDB     AS sysname;  
  
SET @myMergePub = N'AdvWorksSalesOrdersMerge';  
SET @mySub = N'MYSUBSERVER';  
SET @mySubDB = N'AdventureWorksReplica';  
  
-- At the Publisher, register the subscription, using the defaults.  
USE [AdventureWorks]  
EXEC sp_addmergesubscription @publication = @myMergePub,   
@subscriber = @mySub, @subscriber_db = @mySubDB,   
@subscription_type = N'pull';  
GO  

Replikációkezelési objektumok (RMO) használata

Az RMO osztályok, amelyeket a pull előfizetés létrehozásához használnak, attól függnek, milyen kiadványhoz tartozik az előfizetés.

Pull előfizetés létrehozása egy pillanatképhez vagy tranzakciós kiadványhoz

  1. Hozzon létre kapcsolatokat az előfizetővel és a Publisherrel is az ServerConnection osztály használatával.

  2. Példányosítsa az TransPublication osztályt az 1. lépésben használt Publisher-kapcsolat segítségével. Adja meg a Name, DatabaseName és ConnectionContext elemeket.

  3. Hívja meg a LoadProperties metódust. Ha ez a metódus hamis értéket ad vissza, a 2. lépésben megadott tulajdonságok helytelenek, vagy a kiadvány nem létezik a kiszolgálón.

  4. Bitenkénti logikai ÉS művelet végrehajtása (& a Visual C# nyelvben és And a Visual Basic nyelvben) a Attributes tulajdonság és AllowPull között. Ha az eredmény None, állítsa be Attributes értékét az | és közötti bitenkénti logikai OR művelet eredményére (Attributes a Visual C#-ben és AllowPull a Visual Basic-ben). Ezután hívj CommitPropertyChanges fel, hogy engedélyezd a pull előfizetéseket.

  5. Ha az előfizetési adatbázis nem létezik, hozza létre az Database osztály használatával. További információért lásd: Adatbázisok létrehozása, módosítása és eltávolítása.

  6. Hozzon létre egy példányt a TransPullSubscription osztályból.

  7. Adja meg a következő előfizetési tulajdonságokat:

  8. Hívja meg a Create metódust.

  9. A 2. lépésből származó osztály példányát TransPublication használva hívjuk meg a MakePullSubscriptionWellKnown metódust, hogy regisztráljuk a pull előfizetést a Publisher-nél. Ha ez a regisztráció már létezik, kivétel történik.

Pull előfizetés létrehozásához egy merge kiadványhoz

  1. A kaszt használatával ServerConnection hozz létre kapcsolatokat mind az előfizetővel, mind a Publisher-rel.

  2. Példányosítsa az MergePublication osztályt az 1. lépésben használt Publisher-kapcsolat segítségével. Adja meg Namea , DatabaseNameés ConnectionContexta .

  3. Hívja meg a LoadProperties metódust. Ha ez a metódus hamis értéket ad vissza, a 2. lépésben megadott tulajdonságok helytelenek, vagy a kiadvány nem létezik a kiszolgálón.

  4. Bitenkénti logikai ÉS művelet végrehajtása (& a Visual C# nyelvben és And a Visual Basic nyelvben) a Attributes tulajdonság és AllowPull között. Ha az eredmény None, állítsa be Attributes értékét az | és közötti bitenkénti logikai OR művelet eredményére (Attributes a Visual C#-ben és AllowPull a Visual Basic-ben). Ezután hívj CommitPropertyChanges fel, hogy engedélyezd a pull előfizetéseket.

  5. Ha az előfizetési adatbázis nem létezik, hozza létre az Database osztály használatával. További információért lásd: Adatbázisok létrehozása, módosítása és eltávolítása.

  6. Hozzon létre egy példányt a MergePullSubscription osztályból.

  7. Adja meg a következő előfizetési tulajdonságokat:

  8. Hívja meg a Create metódust.

  9. A 2. lépésből származó osztály példányát MergePublication használva hívjuk meg a MakePullSubscriptionWellKnown metódust, hogy regisztráljuk a pull előfizetést a Publisher-nél. Ha ez a regisztráció már létezik, kivétel történik.

Példa (RMO)

Ez a példa pull előfizetést hoz létre egy tranzakciós kiadványhoz. A Microsoft Windows fiók hitelesítő adatait a Distribution Agent feladat létrehozásához használták futásidőben továbbítani.

// Define the Publisher, publication, and databases.  
string publicationName = "AdvWorksProductTran";  
string publisherName = publisherInstance;  
string subscriberName = subscriberInstance;  
string subscriptionDbName = "AdventureWorksReplica";  
string publicationDbName = "AdventureWorks";  
  
//Create connections to the Publisher and Subscriber.  
ServerConnection subscriberConn = new ServerConnection(subscriberName);  
ServerConnection publisherConn = new ServerConnection(publisherName);  
  
// Create the objects that we need.  
TransPublication publication;  
TransPullSubscription subscription;  
  
try  
{  
    // Connect to the Publisher and Subscriber.  
    subscriberConn.Connect();  
    publisherConn.Connect();  
  
    // Ensure that the publication exists and that   
    // it supports pull subscriptions.  
    publication = new TransPublication();  
    publication.Name = publicationName;  
    publication.DatabaseName = publicationDbName;  
    publication.ConnectionContext = publisherConn;  
  
    if (publication.IsExistingObject)  
    {  
        if ((publication.Attributes & PublicationAttributes.AllowPull) == 0)  
        {  
            publication.Attributes |= PublicationAttributes.AllowPull;  
        }  
  
        // Define the pull subscription.  
        subscription = new TransPullSubscription();  
        subscription.ConnectionContext = subscriberConn;  
        subscription.PublisherName = publisherName;  
        subscription.PublicationName = publicationName;  
        subscription.PublicationDBName = publicationDbName;  
        subscription.DatabaseName = subscriptionDbName;  
  
        // Specify the Windows login credentials for the Distribution Agent job.  
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin;  
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword;  
  
        // Make sure that the agent job for the subscription is created.  
        subscription.CreateSyncAgentByDefault = true;  
  
        // 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 pull subscription at the Subscriber.  
        subscription.Create();  
  
        Boolean registered = false;  
  
        // Verify that the subscription is not already registered.  
        foreach (TransSubscription existing  
            in publication.EnumSubscriptions())  
        {  
            if (existing.SubscriberName == subscriberName  
                && existing.SubscriptionDBName == subscriptionDbName)  
            {  
                registered = true;  
            }  
        }  
        if (!registered)  
        {  
            // Register the subscription with the Publisher.  
            publication.MakePullSubscriptionWellKnown(  
                subscriberName, subscriptionDbName,  
                SubscriptionSyncType.Automatic,  
                TransSubscriberType.ReadOnly);  
        }  
    }  
    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  
{  
    subscriberConn.Disconnect();  
    publisherConn.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 = "AdventureWorksReplica"  
Dim publicationDbName As String = "AdventureWorks"  
  
'Create connections to the Publisher and Subscriber.  
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)  
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)  
  
' Create the objects that we need.  
Dim publication As TransPublication  
Dim subscription As TransPullSubscription  
  
Try  
    ' Connect to the Publisher and Subscriber.  
    subscriberConn.Connect()  
    publisherConn.Connect()  
  
    ' Ensure that the publication exists and that   
    ' it supports pull subscriptions.  
    publication = New TransPublication()  
    publication.Name = publicationName  
    publication.DatabaseName = publicationDbName  
    publication.ConnectionContext = publisherConn  
  
    If publication.IsExistingObject Then  
        If (publication.Attributes And PublicationAttributes.AllowPull) = 0 Then  
            publication.Attributes = publication.Attributes _  
            Or PublicationAttributes.AllowPull  
        End If  
  
        ' Define the pull subscription.  
        subscription = New TransPullSubscription()  
        subscription.ConnectionContext = subscriberConn  
        subscription.PublisherName = publisherName  
        subscription.PublicationName = publicationName  
        subscription.PublicationDBName = publicationDbName  
        subscription.DatabaseName = subscriptionDbName  
        subscription.Description = "Pull subscription to " + publicationDbName _  
        + " on " + subscriberName + "."  
  
        ' Specify the Windows login credentials for the Distribution Agent job.  
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin  
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword  
  
        ' Make sure that the agent job for the subscription is created.  
        subscription.CreateSyncAgentByDefault = True  
  
        ' 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 pull subscription at the Subscriber.  
        subscription.Create()  
  
        Dim registered As Boolean = False  
  
        ' Verify that the subscription is not already registered.  
        For Each existing As TransSubscription In publication.EnumSubscriptions()  
            If existing.SubscriberName = subscriberName And _  
                existing.SubscriptionDBName = subscriptionDbName Then  
                registered = True  
            End If  
        Next existing  
        If Not registered Then  
            ' Register the subscription with the Publisher.  
            publication.MakePullSubscriptionWellKnown( _  
             subscriberName, subscriptionDbName, _  
             SubscriptionSyncType.Automatic, _  
             TransSubscriberType.ReadOnly)  
        End If  
    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  
    subscriberConn.Disconnect()  
    publisherConn.Disconnect()  
End Try  
  

Ez a példa pull előfizetést hoz létre egy egyesítő kiadványhoz. A Windows fiók hitelesítő adatait, amelyeket a Merge Agent munka létrehozásához használnak, futásidőben továbbítják.

// Define the Publisher, publication, and databases.  
string publicationName = "AdvWorksSalesOrdersMerge";  
string publisherName = publisherInstance;  
string subscriberName = subscriberInstance;  
string subscriptionDbName = "AdventureWorksReplica";  
string publicationDbName = "AdventureWorks";  
string hostname = @"adventure-works\garrett1";  
  
//Create connections to the Publisher and Subscriber.  
ServerConnection subscriberConn = new ServerConnection(subscriberName);  
ServerConnection publisherConn = new ServerConnection(publisherName);  
  
// Create the objects that we need.  
MergePublication publication;  
MergePullSubscription subscription;  
  
try  
{  
    // Connect to the Subscriber.  
    subscriberConn.Connect();  
  
    // Ensure that the publication exists and that   
    // it supports pull subscriptions.  
    publication = new MergePublication();  
    publication.Name = publicationName;  
    publication.DatabaseName = publicationDbName;  
    publication.ConnectionContext = publisherConn;  
  
    if (publication.LoadProperties())  
    {  
        if ((publication.Attributes & PublicationAttributes.AllowPull) == 0)  
        {  
            publication.Attributes |= PublicationAttributes.AllowPull;  
        }  
  
        // Define the pull subscription.  
        subscription = new MergePullSubscription();  
        subscription.ConnectionContext = subscriberConn;  
        subscription.PublisherName = publisherName;  
        subscription.PublicationName = publicationName;  
        subscription.PublicationDBName = publicationDbName;  
        subscription.DatabaseName = subscriptionDbName;  
        subscription.HostName = hostname;  
  
        // Specify the Windows login credentials for the Merge Agent job.  
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin;  
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword;  
  
        // Make sure that the agent job for the subscription is created.  
        subscription.CreateSyncAgentByDefault = true;  
  
        // Create the pull subscription at the Subscriber.  
        subscription.Create();  
  
        Boolean registered = false;  
  
        // Verify that the subscription is not already registered.  
        foreach (MergeSubscription existing  
            in publication.EnumSubscriptions())  
        {  
            if (existing.SubscriberName == subscriberName  
                && existing.SubscriptionDBName == subscriptionDbName  
                && existing.SubscriptionType == SubscriptionOption.Pull)  
            {  
                registered = true;  
            }  
        }  
        if (!registered)  
        {  
            // Register the local subscription with the Publisher.  
            publication.MakePullSubscriptionWellKnown(  
                subscriberName, subscriptionDbName,  
                SubscriptionSyncType.Automatic,  
                MergeSubscriberType.Local, 0);  
        }  
    }  
    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  
{  
    subscriberConn.Disconnect();  
    publisherConn.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 = "AdventureWorksReplica"  
Dim publicationDbName As String = "AdventureWorks"  
Dim hostname As String = "adventure-works\garrett1"  
  
'Create connections to the Publisher and Subscriber.  
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)  
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)  
  
' Create the objects that we need.  
Dim publication As MergePublication  
Dim subscription As MergePullSubscription  
  
Try  
    ' Connect to the Subscriber.  
    subscriberConn.Connect()  
  
    ' Ensure that the publication exists and that   
    ' it supports pull subscriptions.  
    publication = New MergePublication()  
    publication.Name = publicationName  
    publication.DatabaseName = publicationDbName  
    publication.ConnectionContext = publisherConn  
  
    If publication.LoadProperties() Then  
        If (publication.Attributes And PublicationAttributes.AllowPull) = 0 Then  
            publication.Attributes = publication.Attributes _  
            Or PublicationAttributes.AllowPull  
        End If  
  
        ' Define the pull subscription.  
        subscription = New MergePullSubscription()  
        subscription.ConnectionContext = subscriberConn  
        subscription.PublisherName = publisherName  
        subscription.PublicationName = publicationName  
        subscription.PublicationDBName = publicationDbName  
        subscription.DatabaseName = subscriptionDbName  
        subscription.HostName = hostname  
  
        ' Specify the Windows login credentials for the Merge Agent job.  
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin  
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword  
  
        ' Make sure that the agent job for the subscription is created.  
        subscription.CreateSyncAgentByDefault = True  
  
        ' Create the pull subscription at the Subscriber.  
        subscription.Create()  
  
        Dim registered As Boolean = False  
  
        ' Verify that the subscription is not already registered.  
        For Each existing As MergeSubscription In _  
        publication.EnumSubscriptions()  
            If existing.SubscriberName = subscriberName Then  
                registered = True  
            End If  
        Next  
        If Not registered Then  
            ' Register the local subscription with the Publisher.  
            publication.MakePullSubscriptionWellKnown( _  
             subscriberName, subscriptionDbName, _  
             SubscriptionSyncType.Automatic, _  
             MergeSubscriberType.Local, 0)  
        End If  
    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  
    subscriberConn.Disconnect()  
    publisherConn.Disconnect()  
End Try  

Ez a példa pull előfizetést hoz létre egy egyesítő kiadványhoz anélkül, hogy MSsubscription_properties-ben hozzák létre hozzá tartozó ügynök feladatot és előfizetési metaadatot. A Windows fiók hitelesítő adatait, amelyeket a Merge Agent munka létrehozásához használnak, futásidőben továbbítják.

// Define the Publisher, publication, and databases.  
string publicationName = "AdvWorksSalesOrdersMerge";  
string publisherName = publisherInstance;  
string subscriberName = subscriberInstance;  
string subscriptionDbName = "AdventureWorksReplica";  
string publicationDbName = "AdventureWorks";  
  
//Create connections to the Publisher and Subscriber.  
ServerConnection subscriberConn = new ServerConnection(subscriberName);  
ServerConnection publisherConn = new ServerConnection(publisherName);  
  
// Create the objects that we need.  
MergePublication publication;  
MergePullSubscription subscription;  
  
try  
{  
    // Connect to the Subscriber.  
    subscriberConn.Connect();  
  
    // Ensure that the publication exists and that   
    // it supports pull subscriptions.  
    publication = new MergePublication();  
    publication.Name = publicationName;  
    publication.DatabaseName = publicationDbName;  
    publication.ConnectionContext = publisherConn;  
  
    if (publication.LoadProperties())  
    {  
        if ((publication.Attributes & PublicationAttributes.AllowPull) == 0)  
        {  
            publication.Attributes |= PublicationAttributes.AllowPull;  
        }  
  
        // Define the pull subscription.  
        subscription = new MergePullSubscription();  
        subscription.ConnectionContext = subscriberConn;  
        subscription.PublisherName = publisherName;  
        subscription.PublicationName = publicationName;  
        subscription.PublicationDBName = publicationDbName;  
        subscription.DatabaseName = subscriptionDbName;  
  
        // Specify that an agent job not be created for this subscription. The  
        // subscription can only be synchronized by running the Merge Agent directly.  
        // Subscription metadata stored in MSsubscription_properties will not  
        // be available and must be specified at run time.  
        subscription.CreateSyncAgentByDefault = false;  
  
        // Create the pull subscription at the Subscriber.  
        subscription.Create();  
  
        Boolean registered = false;  
  
        // Verify that the subscription is not already registered.  
        foreach (MergeSubscription existing  
            in publication.EnumSubscriptions())  
        {  
            if (existing.SubscriberName == subscriberName  
                && existing.SubscriptionDBName == subscriptionDbName  
                && existing.SubscriptionType == SubscriptionOption.Pull)  
            {  
                registered = true;  
            }  
        }  
        if (!registered)  
        {  
            // Register the local subscription with the Publisher.  
            publication.MakePullSubscriptionWellKnown(  
                subscriberName, subscriptionDbName,  
                SubscriptionSyncType.Automatic,  
                MergeSubscriberType.Local, 0);  
        }  
    }  
    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  
{  
    subscriberConn.Disconnect();  
    publisherConn.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 = "AdventureWorksReplica"  
Dim publicationDbName As String = "AdventureWorks"  
  
'Create connections to the Publisher and Subscriber.  
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)  
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)  
  
' Create the objects that we need.  
Dim publication As MergePublication  
Dim subscription As MergePullSubscription  
  
Try  
    ' Connect to the Subscriber.  
    subscriberConn.Connect()  
  
    ' Ensure that the publication exists and that   
    ' it supports pull subscriptions.  
    publication = New MergePublication()  
    publication.Name = publicationName  
    publication.DatabaseName = publicationDbName  
    publication.ConnectionContext = publisherConn  
  
    If publication.LoadProperties() Then  
        If (publication.Attributes And PublicationAttributes.AllowPull) = 0 Then  
            publication.Attributes = publication.Attributes _  
            Or PublicationAttributes.AllowPull  
        End If  
  
        ' Define the pull subscription.  
        subscription = New MergePullSubscription()  
        subscription.ConnectionContext = subscriberConn  
        subscription.PublisherName = publisherName  
        subscription.PublicationName = publicationName  
        subscription.PublicationDBName = publicationDbName  
        subscription.DatabaseName = subscriptionDbName  
  
        ' Specify that an agent job not be created for this subscription. The  
        ' subscription can only be synchronized by running the Merge Agent directly.  
        ' Subscription metadata stored in MSsubscription_properties will not  
        ' be available and must be specified at run time.  
        subscription.CreateSyncAgentByDefault = False  
  
        ' Create the pull subscription at the Subscriber.  
        subscription.Create()  
  
        Dim registered As Boolean = False  
  
        ' Verify that the subscription is not already registered.  
        For Each existing As MergeSubscription In _  
        publication.EnumSubscriptions()  
            If existing.SubscriberName = subscriberName Then  
                registered = True  
            End If  
        Next  
        If Not registered Then  
            ' Register the local subscription with the Publisher.  
            publication.MakePullSubscriptionWellKnown( _  
             subscriberName, subscriptionDbName, _  
             SubscriptionSyncType.Automatic, _  
             MergeSubscriberType.Local, 0)  
        End If  
    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  
    subscriberConn.Disconnect()  
    publisherConn.Disconnect()  
End Try  

Ez a példa egy pull előfizetést hoz létre egy egyesült kiadványhoz, amely interneten keresztül szinkronizálható webes szinkronizációval. A Windows fiók hitelesítő adatait, amelyeket a Merge Agent munka létrehozásához használnak, futásidőben továbbítják. További információért lásd: Web szinkronizáció konfigurálása.

// Define the Publisher, publication, and databases.  
string publicationName = "AdvWorksSalesOrdersMerge";  
string publisherName = publisherInstance;  
string subscriberName = subscriberInstance;  
string subscriptionDbName = "AdventureWorksReplica";  
string publicationDbName = "AdventureWorks";  
string hostname = @"adventure-works\garrett1";  
string webSyncUrl = "https://" + publisherInstance + "/WebSync/replisapi.dll";  
  
//Create connections to the Publisher and Subscriber.  
ServerConnection subscriberConn = new ServerConnection(subscriberName);  
ServerConnection publisherConn = new ServerConnection(publisherName);  
  
// Create the objects that we need.  
MergePublication publication;  
MergePullSubscription subscription;  
  
try  
{  
    // Connect to the Subscriber.  
    subscriberConn.Connect();  
  
    // Ensure that the publication exists and that   
    // it supports pull subscriptions and Web synchronization.  
    publication = new MergePublication();  
    publication.Name = publicationName;  
    publication.DatabaseName = publicationDbName;  
    publication.ConnectionContext = publisherConn;  
  
    if (publication.LoadProperties())  
    {  
        if ((publication.Attributes & PublicationAttributes.AllowPull) == 0)  
        {  
            publication.Attributes |= PublicationAttributes.AllowPull;  
        }  
        if ((publication.Attributes & PublicationAttributes.AllowWebSynchronization) == 0)  
        {  
            publication.Attributes |= PublicationAttributes.AllowWebSynchronization;  
        }  
  
        // Define the pull subscription.  
        subscription = new MergePullSubscription();  
        subscription.ConnectionContext = subscriberConn;  
        subscription.PublisherName = publisherName;  
        subscription.PublicationName = publicationName;  
        subscription.PublicationDBName = publicationDbName;  
        subscription.DatabaseName = subscriptionDbName;  
        subscription.HostName = hostname;  
  
        // Specify the Windows login credentials for the Merge Agent job.  
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin;  
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword;  
  
        // Enable Web synchronization.  
        subscription.UseWebSynchronization = true;  
        subscription.InternetUrl = webSyncUrl;  
  
        // Specify the same Windows credentials to use when connecting to the  
        // Web server using HTTPS Basic Authentication.  
        subscription.InternetSecurityMode = AuthenticationMethod.BasicAuthentication;  
        subscription.InternetLogin = winLogin;  
        subscription.InternetPassword = winPassword;  
  
        // Ensure that we create a job for this subscription.  
        subscription.CreateSyncAgentByDefault = true;  
  
        // Create the pull subscription at the Subscriber.  
        subscription.Create();  
  
        Boolean registered = false;  
  
        // Verify that the subscription is not already registered.  
        foreach (MergeSubscription existing  
            in publication.EnumSubscriptions())  
        {  
            if (existing.SubscriberName == subscriberName  
                && existing.SubscriptionDBName == subscriptionDbName  
                && existing.SubscriptionType == SubscriptionOption.Pull)  
            {  
                registered = true;  
            }  
        }  
        if (!registered)  
        {  
            // Register the local subscription with the Publisher.  
            publication.MakePullSubscriptionWellKnown(  
                subscriberName, subscriptionDbName,  
                SubscriptionSyncType.Automatic,  
                MergeSubscriberType.Local, 0);  
        }  
    }  
    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  
{  
    subscriberConn.Disconnect();  
    publisherConn.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 = "AdventureWorksReplica"  
Dim publicationDbName As String = "AdventureWorks"  
Dim hostname As String = "adventure-works\garrett1"  
Dim webSyncUrl As String = "https://" + publisherInstance + "/WebSync/replisapi.dll"  
  
'Create connections to the Publisher and Subscriber.  
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)  
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)  
  
' Create the objects that we need.  
Dim publication As MergePublication  
Dim subscription As MergePullSubscription  
  
Try  
    ' Connect to the Subscriber.  
    subscriberConn.Connect()  
  
    ' Ensure that the publication exists and that   
    ' it supports pull subscriptions and Web synchronization.  
    publication = New MergePublication()  
    publication.Name = publicationName  
    publication.DatabaseName = publicationDbName  
    publication.ConnectionContext = publisherConn  
  
    If publication.LoadProperties() Then  
        If (publication.Attributes And PublicationAttributes.AllowPull) = 0 Then  
            publication.Attributes = publication.Attributes _  
            Or PublicationAttributes.AllowPull  
        End If  
        If (publication.Attributes And PublicationAttributes.AllowWebSynchronization) = 0 Then  
            publication.Attributes = publication.Attributes _  
            Or PublicationAttributes.AllowWebSynchronization  
        End If  
  
        ' Define the pull subscription.  
        subscription = New MergePullSubscription()  
        subscription.ConnectionContext = subscriberConn  
        subscription.PublisherName = publisherName  
        subscription.PublicationName = publicationName  
        subscription.PublicationDBName = publicationDbName  
        subscription.DatabaseName = subscriptionDbName  
        subscription.HostName = hostname  
        subscription.CreateSyncAgentByDefault = True  
  
        ' Specify the Windows login credentials for the Merge Agent job.  
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin  
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword  
  
        ' Enable Web synchronization.  
        subscription.UseWebSynchronization = True  
        subscription.InternetUrl = webSyncUrl  
  
        ' Specify the same Windows credentials to use when connecting to the  
        ' Web server using HTTPS Basic Authentication.  
        subscription.InternetSecurityMode = AuthenticationMethod.BasicAuthentication  
        subscription.InternetLogin = winLogin  
        subscription.InternetPassword = winPassword  
  
        ' Create the pull subscription at the Subscriber.  
        subscription.Create()  
  
        Dim registered As Boolean = False  
  
        ' Verify that the subscription is not already registered.  
        For Each existing As MergeSubscription In _  
        publication.EnumSubscriptions()  
            If existing.SubscriberName = subscriberName Then  
                registered = True  
            End If  
        Next  
        If Not registered Then  
            ' Register the local subscription with the Publisher.  
            publication.MakePullSubscriptionWellKnown( _  
             subscriberName, subscriptionDbName, _  
             SubscriptionSyncType.Automatic, _  
             MergeSubscriberType.Local, 0)  
        End If  
    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  
    subscriberConn.Disconnect()  
    publisherConn.Disconnect()  
End Try