次の方法で共有


プル サブスクリプションを同期する

このトピックでは、SQL Server Management Studio、 レプリケーション エージェント、またはレプリケーション管理オブジェクト (RMO) を使用して、SQL Server 2014 でプル サブスクリプションを同期する方法について説明します。

SQL Server Management Studio の使用

サブスクリプションは、ディストリビューション エージェント (スナップショット レプリケーションとトランザクション レプリケーションの場合) またはマージ エージェント (マージ レプリケーションの場合) によって同期されます。 エージェントは、継続的に実行したり、オンデマンドで実行したり、スケジュールに従って実行したりできます。 同期スケジュールの指定の詳細については、「同期スケジュールの 指定」を参照してください。

SQL Server Management Studio の [ローカル サブスクリプション] フォルダーからオンデマンドでサブスクリプションを同期します。

Management Studio でプル サブスクリプションをオンデマンドで同期するには

  1. Management Studio でサブスクライバーに接続し、サーバー ノードを展開します。

  2. [レプリケーション] フォルダーを展開し、[ローカル サブスクリプション] フォルダーを展開します。

  3. 同期するサブスクリプションを右クリックし、[ 同期状態の表示] をクリックします。

  4. [ 同期状態の表示 - <Subscriber>:<SubscriptionDatabase> ] ダイアログ ボックスで、[ スタート] をクリックします。 同期が完了すると、同期が 完了 したというメッセージが表示されます。

  5. をクリックしてを閉じます。

レプリケーション エージェント

プル サブスクリプションは、コマンド プロンプトから適切なレプリケーション エージェント実行可能ファイルを呼び出すことによって、プログラムとオンデマンドで同期できます。 呼び出されるレプリケーション エージェントの実行可能ファイルは、プル サブスクリプションが属するパブリケーションの種類によって異なります。 詳細については、「 レプリケーション エージェント」を参照してください。

レプリケーション エージェントは、コマンド プロンプトからエージェントを起動したユーザーの Windows 認証資格情報を使用してローカル サーバーに接続します。 これらの Windows 資格情報は、Windows 統合認証を使用してリモート サーバーに接続するときにも使用されます。

コマンド プロンプトまたはバッチ ファイルからディストリビューション エージェントを開始するには

  1. コマンド プロンプトまたはバッチ ファイルで、次のコマンド ライン引数を指定して、distrib.exeを実行してレプリケーションディストリビューションエージェントを起動します。

    • -発行者

    • -PublisherDB

    • -ディストリビューター

    • -DistributorSecurityMode = 1

    • -購読者

    • -SubscriberDB

    • -SubscriberSecurityMode = 1

    • -SubscriptionType = 1

    SQL Server 認証を使用している場合は、次の引数も指定する必要があります。

    • -DistributorLogin

    • -ディストリビューターパスワード

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -SubscriberSecurityMode = 0

コマンド プロンプトまたはバッチ ファイルからマージ エージェントを開始するには

  1. コマンド プロンプトまたはバッチ ファイルで、次のコマンド ライン引数を指定して、replmerg.exeを実行してレプリケーション マージ エージェントを起動します。

    • -発行者

    • -PublisherDB

    • -PublisherSecurityMode = 1

    • -出版

    • -ディストリビューター

    • -DistributorSecurityMode = 1

    • -加入者

    • -サブスクライバーセキュリティモード = 1

    • -SubscriberDB

    • -SubscriptionType = 1

    SQL Server 認証を使用している場合は、次の引数も指定する必要があります。

    • -DistributorLogin

    • -ディストリビューターパスワード

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -サブスクライバーセキュリティモード = 0

例 (レプリケーション エージェント)

次の例では、ディストリビューション エージェントを起動してプル サブスクリプションを同期します。 すべての接続は、Windows 認証を使用して行われます。

SET instancename=USERTEST2

REM<snippetbat_synctranpullsub_10>
REM -- Declare the variables.
SET Publisher=%instancename%
SET Subscriber=%instancename%
SET PublicationDB=AdventureWorks2012
SET SubscriptionDB=AdventureWorks2012Replica 
SET Publication=AdvWorksProductsTran

REM -- Start the Distribution Agent.
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 1
REM</snippetbat_synctranpullsub_10>

PAUSE

次の例では、マージ エージェントを起動してプル サブスクリプションを同期します。 すべての接続は、Windows 認証を使用して行われます。

@ECHO OFF
SET instancename=USERTEST2

REM<snippetbat_syncmergepullsub_10>
REM -- Declare the variables.
SET Publisher=%instancename%
SET Subscriber=%instancename%
SET PublicationDB=AdventureWorks2012
SET SubscriptionDB=AdventureWorks2012Replica 
SET Publication=AdvWorksSalesOrdersMerge

REM --Start the Merge Agent with concurrent upload and download processes.
REM -- The following command must be supplied without line breaks.
"C:\Program Files\Microsoft SQL Server\120\COM\REPLMERG.EXE" -Publication %Publication%  
-Publisher %Publisher%  -Subscriber  %Subscriber%  -Distributor %Publisher%  
-PublisherDB %PublicationDB%  -SubscriberDB %SubscriptionDB% -PublisherSecurityMode 1  
-OutputVerboseLevel 2  -SubscriberSecurityMode 1  -SubscriptionType 1 -DistributorSecurityMode 1  
-Validate 3  -ParallelUploadDownload 1 
REM</snippetbat_syncmergepullsub_10> 

PAUSE

レプリケーション管理オブジェクト (RMO) の使用

レプリケーション管理オブジェクト (RMO) とマネージド コードを使用して、レプリケーション エージェントの機能にアクセスすることで、プル サブスクリプションをプログラムで同期できます。 プル サブスクリプションの同期に使用するクラスは、サブスクリプションが属するパブリケーションの種類によって異なります。

アプリケーションに影響を与えずに自律的に実行される同期を開始する場合は、エージェントを非同期的に開始します。 ただし、同期の結果を監視し、同期プロセス中にエージェントからコールバックを受信する場合 (進行状況バーを表示する場合など)、エージェントを同期的に開始する必要があります。 MicrosoftSQL Server 2005 Express Edition サブスクライバーの場合は、エージェントを同期的に開始する必要があります。

プル サブスクリプションをスナップショット パブリケーションまたはトランザクション パブリケーションに同期するには

  1. ServerConnection クラスを使用してサブスクライバーへの接続を作成します。

  2. TransPullSubscription クラスのインスタンスを作成し、次のプロパティを設定します。

  3. LoadProperties メソッドを呼び出して、残りのサブスクリプションプロパティを取得します。 このメソッドが falseを返す場合は、サブスクリプションが存在することを確認します。

  4. 次のいずれかの方法でサブスクライバーでディストリビューション エージェントを起動します。

    • 手順 2 のTransPullSubscriptionのインスタンスで SynchronizeWithJob メソッドを呼び出します。 このメソッドはディストリビューション エージェントを非同期的に起動し、エージェント ジョブの実行中に制御が直ちにアプリケーションに戻ります。 SQL Server 2005 Express Edition サブスクライバーに対してこのメソッドを呼び出すことはできません。また、サブスクリプションが CreateSyncAgentByDefault の値false (既定値) で作成された場合は呼び出せません。

    • SynchronizationAgent プロパティからTransSynchronizationAgent クラスのインスタンスを取得し、Synchronize メソッドを呼び出します。 このメソッドはエージェントを同期的に開始し、制御は実行中のエージェント ジョブに残ります。 同期実行中は、エージェントの実行中に Status イベントを処理できます。

      プル サブスクリプションの作成時にCreateSyncAgentByDefaultfalseの値 (既定値) を指定した場合は、サブスクリプションのエージェント ジョブ関連のメタデータがMSsubscription_propertiesで使用できないため、DistributorDistributorSecurityMode、必要に応じてDistributorLoginDistributorPasswordも指定する必要があります。

プル サブスクリプションをマージ パブリケーションに同期するには

  1. ServerConnection クラスを使用してサブスクライバーへの接続を作成します。

  2. MergePullSubscription クラスのインスタンスを作成し、次のプロパティを設定します。

  3. LoadProperties メソッドを呼び出して、残りのサブスクリプションプロパティを取得します。 このメソッドが falseを返す場合は、サブスクリプションが存在することを確認します。

  4. 次のいずれかの方法でサブスクライバーでマージ エージェントを起動します。

例 (RMO)

この例では、プル サブスクリプションをトランザクション パブリケーションと同期させます。その際、エージェントはエージェント ジョブを用いて非同期的に開始されます。

// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksProductTran";
String publicationDbName = "AdventureWorks2012";
String subscriptionDbName = "AdventureWorks2012Replica";

// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);

TransPullSubscription subscription;

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

    // Define subscription properties.
    subscription = new TransPullSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = subscriptionDbName;
    subscription.PublisherName = publisherName;
    subscription.PublicationDBName = publicationDbName;
    subscription.PublicationName = publicationName;

    // If the pull subscription and the job exists, start the agent job.
    if (subscription.LoadProperties() && subscription.AgentJobId != null)
    {
        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)
{
    // Do appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2012"
Dim subscriptionDbName As String = "AdventureWorks2012Replica"

' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)

Dim subscription As TransPullSubscription

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

    ' Define subscription properties.
    subscription = New TransPullSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = subscriptionDbName
    subscription.PublisherName = publisherName
    subscription.PublicationDBName = publicationDbName
    subscription.PublicationName = publicationName

    ' If the pull subscription and the job exists, start the agent job.
    If subscription.LoadProperties() And Not subscription.AgentJobId Is Nothing Then
        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
    ' Do appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try

この例では、プル サブスクリプションをトランザクション パブリケーションに同期し、エージェントは同期的に開始されます。

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

// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);

TransPullSubscription subscription;

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

    // Define the pull subscription.
    subscription = new TransPullSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = subscriptionDbName;
    subscription.PublisherName = publisherName;
    subscription.PublicationDBName = publicationDbName;
    subscription.PublicationName = publicationName;

    // If the pull subscription exists, then start the synchronization.
    if (subscription.LoadProperties())
    {
        // Check that we have enough metadata to start the agent.
        if (subscription.PublisherSecurity != 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 pull subscription does not exist.
        throw new ApplicationException(String.Format(
            "A 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. Verify that the subscription has " +
        "been defined correctly.", 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 = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"

' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)

Dim subscription As TransPullSubscription

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

    ' Define the pull subscription.
    subscription = New TransPullSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = subscriptionDbName
    subscription.PublisherName = publisherName
    subscription.PublicationDBName = publicationDbName
    subscription.PublicationName = publicationName

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

            ' Write agent output to a log file.
            subscription.SynchronizationAgent.Output = "distagent.log"
            subscription.SynchronizationAgent.OutputVerboseLevel = 2

            ' 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 pull subscription does not exist.
        Throw New ApplicationException(String.Format( _
         "A 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. Verify that the subscription has " + _
     "been defined correctly.", ex)
Finally
    conn.Disconnect()
End Try

次の使用例は、マージ パブリケーションにプル サブスクリプションを同期します。この場合、エージェントはエージェント ジョブを使用して非同期的に開始されます。

// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksSalesOrdersMerge";
String publicationDbName = "AdventureWorks2012";
String subscriptionDbName = "AdventureWorks2012Replica";

// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);

MergePullSubscription subscription;

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

    // Define subscription properties.
    subscription = new MergePullSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = subscriptionDbName;
    subscription.PublisherName = publisherName;
    subscription.PublicationDBName = publicationDbName;
    subscription.PublicationName = publicationName;

    // If the pull subscription and the job exists, start the agent job.
    if (subscription.LoadProperties() && subscription.AgentJobId != null)
    {
        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)
{
    // Do appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks2012"
Dim subscriptionDbName As String = "AdventureWorks2012Replica"

' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)

Dim subscription As MergePullSubscription

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

    ' Define subscription properties.
    subscription = New MergePullSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = subscriptionDbName
    subscription.PublisherName = publisherName
    subscription.PublicationDBName = publicationDbName
    subscription.PublicationName = publicationName

    ' If the pull subscription and the job exists, start the agent job.
    If subscription.LoadProperties() And Not subscription.AgentJobId Is Nothing Then
        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
    ' Do appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try

次の使用例は、マージ パブリケーションにプル サブスクリプションを同期します。この場合、エージェントは同期的に開始されます。

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

// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);

MergePullSubscription subscription;

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

    // Define the pull subscription.
    subscription = new MergePullSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = subscriptionDbName;
    subscription.PublisherName = publisherName;
    subscription.PublicationDBName = publicationDbName;
    subscription.PublicationName = publicationName;

    // If the pull subscription exists, then start the synchronization.
    if (subscription.LoadProperties())
    {
        // Check that we have enough metadata to start the agent.
        if (subscription.PublisherSecurity != null || subscription.DistributorSecurity != 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 pull subscription does not exist.
        throw new ApplicationException(String.Format(
            "A 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. Verify that the subscription has " +
        "been defined correctly.", 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 = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"

' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)

Dim subscription As MergePullSubscription

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

    ' Define the pull subscription.
    subscription = New MergePullSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = subscriptionDbName
    subscription.PublisherName = publisherName
    subscription.PublicationDBName = publicationDbName
    subscription.PublicationName = publicationName

    ' If the pull subscription exists, then start the synchronization.
    If subscription.LoadProperties() Then
        ' Check that we have enough metadata to start the agent.
        If Not subscription.PublisherSecurity Is Nothing Or subscription.DistributorSecurity Is Nothing Then

            ' Output agent messages to the console. 
            subscription.SynchronizationAgent.OutputVerboseLevel = 1
            subscription.SynchronizationAgent.Output = ""

            ' 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.")
        End If
    Else
        ' Do something here if the pull subscription does not exist.
        Throw New ApplicationException(String.Format( _
         "A 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. Verify that the subscription has " + _
     "been defined correctly.", ex)
Finally
    conn.Disconnect()
End Try

次の使用例は、Web 同期を使用してプル サブスクリプションをマージ パブリケーションに同期します。 サブスクリプションはエージェント ジョブと関連するサブスクリプション メタデータなしで作成されたため、エージェントを同期的に開始し、追加のサブスクリプション情報を指定する必要があります。

// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string distributorName = distributorInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string subscriptionDbName = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";
string hostname = @"adventure-works\garrett1";
string webSyncUrl = "https://" + publisherInstance + "/SalesOrders/replisapi.dll";

// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);

MergePullSubscription subscription;
MergeSynchronizationAgent agent;

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

    // Define the pull subscription.
    subscription = new MergePullSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = subscriptionDbName;
    subscription.PublisherName = publisherName;
    subscription.PublicationDBName = publicationDbName;
    subscription.PublicationName = publicationName;

    // If the pull subscription exists, then start the synchronization.
    if (subscription.LoadProperties())
    {
        // Get the agent for the subscription.
        agent = subscription.SynchronizationAgent;

        // Check that we have enough metadata to start the agent.
        if (agent.PublisherSecurityMode == null)
        {
            // Set the required properties that could not be returned
            // from the MSsubscription_properties table. 
            agent.PublisherSecurityMode = SecurityMode.Integrated;
            agent.DistributorSecurityMode = SecurityMode.Integrated;
            agent.Distributor = publisherName;
            agent.HostName = hostname;

            // Set optional Web synchronization properties.
            agent.UseWebSynchronization = true;
            agent.InternetUrl = webSyncUrl;
            agent.InternetSecurityMode = SecurityMode.Standard;
            agent.InternetLogin = winLogin;
            agent.InternetPassword = winPassword;
        }
        // Enable agent output to the console.
        agent.OutputVerboseLevel = 1;
        agent.Output = "";

        // Synchronously start the Merge Agent for the subscription.
        agent.Synchronize();
    }
    else
    {
        // Do something here if the pull subscription does not exist.
        throw new ApplicationException(String.Format(
            "A 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. Verify that the subscription has " +
        "been defined correctly.", 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 = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"
Dim hostname As String = "adventure-works\garrett1"
Dim webSyncUrl As String = "https://" + publisherInstance + "/SalesOrders/replisapi.dll"

' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)

Dim subscription As MergePullSubscription
Dim agent As MergeSynchronizationAgent

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

    ' Define the pull subscription.
    subscription = New MergePullSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = subscriptionDbName
    subscription.PublisherName = publisherName
    subscription.PublicationDBName = publicationDbName
    subscription.PublicationName = publicationName

    ' If the pull subscription exists, then start the synchronization.
    If subscription.LoadProperties() Then
        ' Get the agent for the subscription.
        agent = subscription.SynchronizationAgent

        ' Check that we have enough metadata to start the agent.
        If agent.PublisherSecurityMode = Nothing Then
            ' Set the required properties that could not be returned
            ' from the MSsubscription_properties table. 
            agent.PublisherSecurityMode = SecurityMode.Integrated
            agent.Distributor = publisherInstance
            agent.DistributorSecurityMode = SecurityMode.Integrated
            agent.HostName = hostname

            ' Set optional Web synchronization properties.
            agent.UseWebSynchronization = True
            agent.InternetUrl = webSyncUrl
            agent.InternetSecurityMode = SecurityMode.Standard
            agent.InternetLogin = winLogin
            agent.InternetPassword = winPassword
        End If

        ' Enable agent logging to the console.
        agent.OutputVerboseLevel = 1
        agent.Output = ""

        ' Synchronously start the Merge Agent for the subscription.
        agent.Synchronize()
    Else
        ' Do something here if the pull subscription does not exist.
        Throw New ApplicationException(String.Format( _
         "A 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. Verify that the subscription has " + _
     "been defined correctly.", ex)
Finally
    conn.Disconnect()
End Try

こちらもご覧ください

データの同期
プル サブスクリプションを作成する
レプリケーション のセキュリティに関するベスト プラクティス