次の方法で共有


プル サブスクリプションの同期

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

このトピックの内容

  • プル サブスクリプションを同期するために使用するもの:

    SQL Server Management Studio

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

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

SQL Server Management Studio の使用

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

SQL Server Management Studio の [ローカル サブスクリプション] フォルダーから、サブスクリプションを要求時に同期します。

Management Studio でプル サブスクリプションを要求時に同期するには

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

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

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

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

  5. [閉じる] をクリックします。

[トップに戻る] リンクで使用される矢印アイコン[Top]

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

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

注意

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

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

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

    • -Publisher

    • -PublisherDB

    • -Distributor

    • -DistributorSecurityMode = 1

    • -Subscriber

    • -SubscriberDB

    • -SubscriberSecurityMode = 1

    • -SubscriptionType = 1

    SQL Server 認証を使用する場合は、さらに、次の引数を指定する必要があります。

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -SubscriberSecurityMode = 0

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

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

    • -Publisher

    • -PublisherDB

    • -PublisherSecurityMode = 1

    • -Publication

    • -Distributor

    • -DistributorSecurityMode = 1

    • -Subscriber

    • -SubscriberSecurityMode = 1

    • -SubscriberDB

    • -SubscriptionType = 1

    SQL Server 認証を使用する場合は、さらに、次の引数を指定する必要があります。

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -SubscriberSecurityMode = 0

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

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

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\110\COM\DISTRIB.EXE" -Subscriber %Subscriber% 
-SubscriberDB %SubscriptionDB% -SubscriberSecurityMode 1 -Publication %Publication% 
-Publisher %Publisher% -PublisherDB %PublicationDB% -Distributor %Publisher% 
-DistributorSecurityMode 1 -Continuous -SubscriptionType 1

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

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\110\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 

[トップに戻る] リンクで使用される矢印アイコン[Top]

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

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

注意

アプリケーションに影響を及ぼすことなく、自立的に実行される同期を開始するには、エージェントを非同期的に起動します。 ただし、同期処理中に同期の結果を監視し、エージェントからのコールバックを受け取る場合 (たとえば、進行状況バーを表示する場合)、エージェントを同期的に起動する必要があります。 Microsoft SQL Server 2005 Express Edition サブスクライバーの場合、エージェントを同期的に起動する必要があります。

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

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

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

    • DatabaseName に、サブスクリプション データベースの名前を設定します。

    • PublicationName に、サブスクリプションが属しているパブリケーションの名前を設定します。

    • PublicationDBName にパブリケーション データベース名を指定します。

    • PublisherName に、パブリッシャーの名前を設定します。

    • ConnectionContext に、手順 1. で作成した接続を設定します。

  3. LoadProperties メソッドを呼び出して、その他のサブスクリプション プロパティを取得します。 このメソッドが false を返す場合、サブスクリプションが存在するかどうかを確認してください。

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

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

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

      注意

      プル サブスクリプションの作成時、CreateSyncAgentByDefault の値に false (既定値) を指定した場合、サブスクリプションのエージェント ジョブに関連したメタデータを MSsubscription_properties から取得することができないため、Distributor および DistributorSecurityMode を指定し、さらに、必要に応じて DistributorLoginDistributorPassword も指定する必要があります。

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

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

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

    • DatabaseName に、サブスクリプション データベースの名前を設定します。

    • PublicationName に、サブスクリプションが属しているパブリケーションの名前を設定します。

    • パブリッシュするデータベースの名前を PublicationDBName に指定します。

    • PublisherName に、パブリッシャーの名前を設定します。

    • ConnectionContext に、手順 1. で作成した接続を設定します。

  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

[トップに戻る] リンクで使用される矢印アイコン[Top]

関連項目

概念

データの同期

プル サブスクリプションの作成

レプリケーション セキュリティの推奨事項