如何创建初始快照(RMO 编程)

快照代理将在创建发布后生成快照。 可以使用复制管理对象 (RMO) 和直接托管代码对复制代理功能的访问权限以编程的方式生成这些快照。 所使用的对象取决于复制的类型。 可以使用 SnapshotGenerationAgent 对象同步启动快照代理,也可以使用代理作业异步启动快照代理。 初始快照生成后,该快照将在订阅首次同步时传输并应用到订阅服务器。 只要现有快照不再包含有效的最新数据,您就需要重新运行代理。 有关详细信息,请参阅维护发布

使用参数化筛选器的合并发布需要由两部分组成的快照。 有关详细信息,请参阅如何创建具有参数化筛选器的合并发布的快照(RMO 编程)

安全说明安全说明

如果可能,请在运行时提示用户输入安全凭据。 如果必须存储凭据,请使用 Microsoft Windows .NET Framework 提供的 Cryptographic Services(加密服务)。

通过启动快照代理作业(异步)为快照发布或事务发布生成初始快照

  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 = "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