如何:建立初始快照集 (RMO 程式設計)

快照集代理程式會在發行集建立之後產生快照集。您可以使用「複寫管理物件」(RMO) 和對複寫代理程式功能的直接 Managed 程式碼存取,以程式設計的方式產生這些快照集。您使用的物件取決於複寫的類型而定。您可以使用 SnapshotGenerationAgent 物件同步啟動快照集代理程式,或是使用代理程式作業以非同步方式啟動它。在產生初始快照集之後,此快照集會在第一次同步處理訂閱時,傳送及套用到訂閱者。每當現有的快照集不再包含有效且最新的資料時,您就需要重新執行此代理程式。如需詳細資訊,請參閱<維護發行集>。

使用參數化篩選的合併式發行集需要一個兩段式快照集。如需詳細資訊,請參閱<如何:使用參數化篩選建立合併式發行集的快照集 (RMO 程式設計)>。

安全性注意事項安全性注意事項

可能的話,系統會在執行階段提示使用者輸入安全性認證。如果您必須儲存認證,請使用 Microsoft Windows .NET Framework 提供的<密碼編譯服務>(英文)。

啟動快照集代理程式作業 (非同步) 來針對快照式或交易式發行集產生初始快照集

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

  2. 建立 TransPublication 類別的執行個體。設定發行集的 NameDatabaseName 屬性,並將 ConnectionContext 屬性設定為在步驟 1 中建立的連接。

  3. 呼叫 LoadProperties 方法以載入物件的剩餘屬性。如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。

  4. 如果 SnapshotAgentExists 的值是 false,請呼叫 CreateSnapshotAgent 來針對這個發行集建立快照集代理程式作業。

  5. 呼叫 StartSnapshotGenerationAgentJob 方法來啟動代理程式作業,以針對這個發行集產生快照集。

  6. (選擇性) 當 SnapshotAvailable 的值是 true 時,表示快照集可供訂閱者使用。

執行快照集代理程式 (同步) 來針對快照式或交易式發行集產生初始快照集

  1. 建立 SnapshotGenerationAgent 類別的執行個體,並設定下列必要的屬性:

  2. 針對 ReplicationType 設定 TransactionalSnapshot 的值。

  3. 呼叫 GenerateSnapshot 方法。

啟動快照集代理程式作業 (非同步) 來針對合併式發行集產生初始快照集

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

  2. 建立 MergePublication 類別的執行個體。設定發行集的 NameDatabaseName 屬性,並將 ConnectionContext 屬性設定為在步驟 1 中建立的連接。

  3. 呼叫 LoadProperties 方法以載入物件的剩餘屬性。如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。

  4. 如果 SnapshotAgentExists 的值是 false,請呼叫 CreateSnapshotAgent 來針對這個發行集建立快照集代理程式作業。

  5. 呼叫 StartSnapshotGenerationAgentJob 方法來啟動代理程式作業,以針對這個發行集產生快照集。

  6. (選擇性) 當 SnapshotAvailable 的值是 true 時,表示快照集可供訂閱者使用。

執行快照集代理程式 (同步) 來針對合併式發行集產生初始快照集

  1. 建立 SnapshotGenerationAgent 類別的執行個體,並設定下列必要的屬性:

  2. ReplicationType 設定 Merge 的值。

  3. 呼叫 GenerateSnapshot 方法。

範例

這個範例會同步執行快照集代理程式,以針對交易式發行集產生初始快照集。

           // Set the Publisher, publication database, and publication names.
            string publicationName = "AdvWorksProductTran";
            string publicationDbName = "AdventureWorks2008R2";
            string publisherName = publisherInstance;
            string distributorName = publisherInstance;

            SnapshotGenerationAgent agent;

            try
            {
                // Set the required properties for Snapshot Agent.
                agent = new SnapshotGenerationAgent();
                agent.Distributor = distributorName;
                agent.DistributorSecurityMode = SecurityMode.Integrated;
                agent.Publisher = publisherName;
                agent.PublisherSecurityMode = SecurityMode.Integrated;
                agent.Publication = publicationName;
                agent.PublisherDatabase = publicationDbName;
                agent.ReplicationType = ReplicationType.Transactional;

                // Start the agent synchronously.
                agent.GenerateSnapshot();

            }
            catch (Exception ex)
            {
                // Implement custom application error handling here.
                throw new ApplicationException(String.Format(
                    "A snapshot could not be generated for the {0} publication."
                    , publicationName), ex);
            }
' Set the Publisher, publication database, and publication names.
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2008R2"
Dim publisherName As String = publisherInstance
Dim distributorName As String = publisherInstance

Dim agent As SnapshotGenerationAgent

Try
    ' Set the required properties for Snapshot Agent.
    agent = New SnapshotGenerationAgent()
    agent.Distributor = distributorName
    agent.DistributorSecurityMode = SecurityMode.Integrated
    agent.Publisher = publisherName
    agent.PublisherSecurityMode = SecurityMode.Integrated
    agent.Publication = publicationName
    agent.PublisherDatabase = publicationDbName
    agent.ReplicationType = ReplicationType.Transactional

    ' Start the agent synchronously.
    agent.GenerateSnapshot()

Catch ex As Exception
    ' Implement custom application error handling here.
    Throw New ApplicationException(String.Format( _
     "A snapshot could not be generated for the {0} publication." _
     , publicationName), ex)
End Try

這個範例會以非同步方式啟動代理程式作業,以針對交易式發行集產生初始快照集。

           // Set the Publisher, publication database, and publication names.
            string publicationName = "AdvWorksProductTran";
            string publicationDbName = "AdventureWorks2008R2";
            string publisherName = publisherInstance;

            TransPublication publication;

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

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

                // Set the required properties for an existing publication.
                publication = new TransPublication();
                publication.ConnectionContext = conn;
                publication.Name = publicationName;
                publication.DatabaseName = publicationDbName;

                if (publication.LoadProperties())
                {
                    // Start the Snapshot Agent job for the publication.
                    publication.StartSnapshotGenerationAgentJob();
                }
                else
                {
                    throw new ApplicationException(String.Format(
                        "The {0} publication does not exist.", publicationName));
                }
            }
            catch (Exception ex)
            {
                // Implement custom application error handling here.
                throw new ApplicationException(String.Format(
                    "A snapshot could not be generated for the {0} publication."
                    , publicationName), ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Set the Publisher, publication database, and publication names.
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2008R2"
Dim publisherName As String = publisherInstance

Dim publication As TransPublication

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

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

    ' Set the required properties for an existing publication.
    publication = New TransPublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    If publication.LoadProperties() Then
        ' Start the Snapshot Agent job for the publication.
        publication.StartSnapshotGenerationAgentJob()
    Else
        Throw New ApplicationException(String.Format( _
         "The {0} publication does not exist.", publicationName))
    End If
Catch ex As Exception
    ' Implement custom application error handling here.
    Throw New ApplicationException(String.Format( _
     "A snapshot could not be generated for the {0} publication." _
     , publicationName), ex)
Finally
    conn.Disconnect()
End Try