共用方式為


如何:重新初始化訂閱 (RMO 程式設計)

個別訂閱可標示為要重新初始化,好讓下一次同步處理期間會套用新的快照集。您可以使用「複寫管理物件」(RMO) 以程式設計的方式重新初始化訂閱。使用的類別取決於訂閱所屬的發行集類型及訂閱的類型 (發送訂閱或提取訂閱) 而定。

重新初始化交易式發行集的提取訂閱

  1. 使用 ServerConnection 類別建立與訂閱者的連接。

  2. 建立 TransPullSubscription 類別的執行個體,並設定 PublicationNameDatabaseNamePublisherNamePublicationDBName 及針對 ConnectionContext 設定步驟 1 中的連接。

  3. 呼叫 LoadProperties 方法以取得物件的屬性。

    [!附註]

    如果此方法傳回 false,則表示步驟 2 中的訂閱屬性定義不正確,或者提取訂閱不存在。

  4. 呼叫 Reinitialize 方法。此方法會標示要重新初始化的訂閱。

  5. 同步處理提取訂閱。如需詳細資訊,請參閱<如何:同步處理提取訂閱 (RMO 程式設計)>。

重新初始化交易式發行集的發送訂閱

  1. 使用 ServerConnection 類別建立與發行者的連接。

  2. 建立 TransSubscription 類別的執行個體,並設定 PublicationNameDatabaseNameSubscriberNameSubscriptionDBName 及針對 ConnectionContext 設定步驟 1 中的連接。

  3. 呼叫 LoadProperties 方法以取得物件的屬性。

    [!附註]

    如果此方法傳回 false,則表示步驟 2 中的訂閱屬性定義不正確,或者發送訂閱不存在。

  4. 呼叫 Reinitialize 方法。此方法會標示要重新初始化的訂閱。

  5. 同步處理發送訂閱。如需詳細資訊,請參閱<如何:同步處理發送訂閱 (RMO 程式設計)>。

重新初始化合併式發行集的提取訂閱

  1. 使用 ServerConnection 類別建立與訂閱者的連接。

  2. 建立 MergePullSubscription 類別的執行個體,並設定 PublicationNameDatabaseNamePublisherNamePublicationDBName 及針對 ConnectionContext 設定步驟 1 中的連接。

  3. 呼叫 LoadProperties 方法以取得物件的屬性。

    [!附註]

    如果此方法傳回 false,則表示步驟 2 中的訂閱屬性定義不正確,或者提取訂閱不存在。

  4. 呼叫 Reinitialize 方法。傳遞 true 的值可在重新初始化之前上載訂閱者上的變更,或者傳遞 false 的值可重新初始化及遺失訂閱者上的任何暫止變更。此方法會標示要重新初始化的訂閱。

    [!附註]

    如果此訂閱已過期,將無法上載變更。如需詳細資訊,請參閱<合併訂閱已過期且必須上載變更>。

  5. 同步處理提取訂閱。如需詳細資訊,請參閱<如何:同步處理提取訂閱 (RMO 程式設計)>。

重新初始化合併式發行集的發送訂閱

  1. 使用 ServerConnection 類別建立與發行者的連接。

  2. 建立 MergeSubscription 類別的執行個體,並設定 PublicationNameDatabaseNameSubscriberNameSubscriptionDBName 及針對 ConnectionContext 設定步驟 1 中的連接。

  3. 呼叫 LoadProperties 方法以取得物件的屬性。

    [!附註]

    如果此方法傳回 false,則表示步驟 2 中的訂閱屬性定義不正確,或者發送訂閱不存在。

  4. 呼叫 Reinitialize 方法。傳遞 true 的值可在重新初始化之前上載訂閱者上的變更,或者傳遞 false 的值可重新初始化及遺失訂閱者上的任何暫止變更。此方法會標示要重新初始化的訂閱。

    [!附註]

    如果此訂閱已過期,將無法上載變更。如需詳細資訊,請參閱<合併訂閱已過期且必須上載變更>。

  5. 同步處理發送訂閱。如需詳細資訊,請參閱<如何:同步處理發送訂閱 (RMO 程式設計)>。

範例

此範例會重新初始化交易式發行集的提取訂閱。

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

            // 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, mark the subscription
                // for reinitialization and start the agent job.
                if (subscription.LoadProperties() && subscription.AgentJobId != null)
                {
                    subscription.Reinitialize();
                    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 reinitialized.", 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 = "AdventureWorks2008R2"
Dim subscriptionDbName As String = "AdventureWorks2008R2Replica"

' 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, mark the subscription
    ' for reinitialization and start the agent job.
    If subscription.LoadProperties() And (Not subscription.AgentJobId Is Nothing) Then
        subscription.Reinitialize()
        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 reinitialized.", ex)
Finally
    conn.Disconnect()
End Try

此範例會在初次上載訂閱者上的暫止變更之後,重新初始化合併式發行集的提取訂閱。

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

            // 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, mark the subscription
                // for reinitialization after upload and start the agent job.
                if (subscription.LoadProperties() && subscription.AgentJobId != null)
                {
                    subscription.Reinitialize(true);
                    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 = "AdventureWorks2008R2"
Dim subscriptionDbName As String = "AdventureWorks2008R2Replica"

' 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, mark the subscription
    ' for reinitialization after upload and start the agent job.
    If subscription.LoadProperties() And (Not subscription.AgentJobId Is Nothing) Then
        subscription.Reinitialize(True)
        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