Share via


初期スナップショットを作成する方法 (RMO プログラミング)

スナップショット エージェントは、パブリッシャが作成された後でスナップショットを生成します。レプリケーション管理オブジェクト (RMO) およびレプリケーション エージェント機能への直接的なマネージ コード アクセスを使用して、これらのスナップショットをプログラムで生成できます。使用するオブジェクトは、レプリケーションの種類によって異なります。スナップショット エージェントを同期的に開始する場合は SnapshotGenerationAgent オブジェクトを使用し、非同期的に開始する場合はエージェント ジョブを使用します。初期スナップショットの生成後、サブスクリプションを最初に同期するときに、初期スナップショットをサブスクライバに転送して適用することができます。既存のスナップショットに最新の有効なデータが含まれていない場合は、エージェントを再実行する必要があります。詳細については、「パブリケーションの管理」を参照してください。

パラメータ化されたフィルタを使用するマージ パブリケーションでは、2 つの部分から成るスナップショットが必要です。詳細については、「パラメータ化されたフィルタを使用してマージ パブリケーションのスナップショットを作成する方法 (RMO プログラミング)」を参照してください。

セキュリティに関する注意セキュリティに関する注意

可能であれば、実行時に、ユーザーに対してセキュリティ資格情報の入力を求めるメッセージを表示します。資格情報を保存する必要がある場合は、Microsoft Windows .NET Framework に用意されている暗号化サービスを使用します。

スナップショット エージェント ジョブを非同期的に開始して、スナップショット パブリケーションまたはトランザクション パブリケーションの初期スナップショットを生成するには

  1. ServerConnection クラスを使用して、パブリッシャへの接続を作成します。

  2. TransPublication クラスのインスタンスを作成します。パブリケーションの Name プロパティおよび DatabaseName プロパティを設定し、ConnectionContext プロパティに手順 1. で作成した接続を設定します。

  3. LoadProperties メソッドを呼び出して、オブジェクトの残りのプロパティを読み込みます。このメソッドが false を返す場合、手順 2. でパブリケーション プロパティを不適切に設定したか、パブリケーションが存在していません。

  4. SnapshotAgentExists の値が false の場合は、CreateSnapshotAgent を呼び出して、このパブリケーション用のスナップショット エージェント ジョブを作成します。

  5. StartSnapshotGenerationAgentJob メソッドを呼び出して、このパブリケーションのスナップショットを生成するエージェント ジョブを開始します。

  6. (省略可) SnapshotAvailable の値が true である場合は、スナップショットをサブスクライバに使用できます。

スナップショット エージェント ジョブを同期的に実行して、スナップショット パブリケーションまたはトランザクション パブリケーションの初期スナップショットを生成するには

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

  2. ReplicationTypeTransactional または Snapshot を設定します。

  3. GenerateSnapshot メソッドを呼び出します。

スナップショット エージェント ジョブを非同期的に開始して、マージ パブリケーションの初期スナップショットを生成するには

  1. ServerConnection クラスを使用して、パブリッシャへの接続を作成します。

  2. MergePublication クラスのインスタンスを作成します。パブリケーションの Name プロパティおよび DatabaseName プロパティを設定し、ConnectionContext プロパティに手順 1. で作成した接続を設定します。

  3. LoadProperties メソッドを呼び出して、オブジェクトの残りのプロパティを読み込みます。このメソッドが false を返す場合、手順 2. でパブリケーション プロパティを不適切に設定したか、パブリケーションが存在していません。

  4. SnapshotAgentExists の値が false の場合は、CreateSnapshotAgent を呼び出して、このパブリケーション用のスナップショット エージェント ジョブを作成します。

  5. StartSnapshotGenerationAgentJob メソッドを呼び出して、このパブリケーションのスナップショットを生成するエージェント ジョブを開始します。

  6. (省略可) SnapshotAvailable の値が true である場合は、スナップショットをサブスクライバに使用できます。

スナップショット エージェントを同期的に実行して、マージ パブリケーションの初期スナップショットを生成するには

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

  2. ReplicationTypeMerge を設定します。

  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