次の方法で共有


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

スナップショット エージェントは、パブリッシャが作成された後でスナップショットを生成します。レプリケーション管理オブジェクト (RMO) およびレプリケーション エージェント機能への直接的なマネージ コード アクセスを使用して、これらのスナップショットをプログラムで生成できます。使用するオブジェクトは、レプリケーションの種類によって異なります。スナップショット エージェントを同期的に開始する場合は SnapshotGenerationAgent オブジェクトを使用し、非同期的に開始する場合はエージェント ジョブを使用します。パラメータ化されたフィルタによるマージ パブリケーションの詳細については、「パラメータ化されたフィルタを使用してマージ パブリケーションのスナップショットを作成する方法 (RMO プログラミング)」を参照してください。

初期スナップショットの生成後、サブスクリプションを最初に同期するときに、初期スナップショットをサブスクライバに転送して適用することができます。既存のスナップショットに最新の有効なデータが含まれていない場合は、エージェントを再実行する必要があります。詳細については、「パブリケーションの管理」を参照してください。

ms147912.security(ja-jp,SQL.90).gifセキュリティ メモ :
可能であれば、実行時に、ユーザーに対してセキュリティ資格情報の入力を求めるメッセージを表示します。資格情報を保存する必要がある場合は、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 クラスのインスタンスを作成し、次の必須プロパティを設定します。

    • Publisher - パブリッシャの名前
    • PublisherDatabase - パブリケーション データベースの名前
    • Publication - パブリケーションの名前
    • Distributor - ディストリビュータの名前
    • PublisherSecurityMode - パブリッシャへの接続時に Windows 認証を使用する場合は Integrated を指定します。パブリッシャへの接続時に SQL Server 認証を使用する場合は Standard を指定し、PublisherLogin および PublisherPassword に値を指定します。推奨されるのは、Windows 認証です。
    • DistributorSecurityMode - ディストリビュータへの接続時に Windows 認証を使用する場合は Integrated を指定します。ディストリビュータへの接続時に SQL Server 認証を使用する場合は Standard を指定し、DistributorLogin および DistributorPassword に値を指定します。推奨されるのは、Windows 認証です。
  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 クラスのインスタンスを作成し、次の必須プロパティを設定します。

    • Publisher - パブリッシャの名前
    • PublisherDatabase - パブリケーション データベースの名前
    • Publication - パブリケーションの名前
    • Distributor - ディストリビュータの名前
    • PublisherSecurityMode - パブリッシャへの接続時に Windows 認証を使用する場合は Integrated を指定します。パブリッシャへの接続時に SQL Server 認証を使用する場合は Standard を指定し、PublisherLogin および PublisherPassword に値を指定します。推奨されるのは、Windows 認証です。
    • DistributorSecurityMode - ディストリビュータへの接続時に Windows 認証を使用する場合は Integrated を指定します。ディストリビュータへの接続時に SQL Server 認証を使用する場合は Standard を指定し、DistributorLogin および DistributorPassword に値を指定します。推奨されるのは、Windows 認証です。
  2. ReplicationTypeMerge を設定します。

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

使用例

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

// Set the Publisher, publication database, and publication names.
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks";
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 = "AdventureWorks"
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 = "AdventureWorks";
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 = "AdventureWorks"
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

参照

処理手順

初期スナップショットを作成する方法 (レプリケーション Transact-SQL プログラミング)

概念

レプリケーション管理オブジェクトを使用したプログラミング

その他の技術情報

初期スナップショットを作成および適用する方法 (SQL Server Management Studio)
スナップショットを使用したサブスクリプションの初期化

ヘルプおよび情報

SQL Server 2005 の参考資料の入手