如何验证订阅服务器上的数据(RMO 编程)

通过复制,您可以使用复制管理对象 (RMO) 以编程方式验证订阅服务器上的数据是否与发布服务器上的数据匹配。 使用的对象取决于复制拓扑的类型。 事务复制要求验证发布的所有订阅。

验证事务发布中所有项目的数据

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 TransPublication 类的实例。 设置发布的 NameDatabaseName 属性。 将 ConnectionContext 属性设置为步骤 1 中创建的连接。

  3. 调用 LoadProperties 方法获取该对象的其余属性。 如果此方法返回 false,则说明步骤 2 中的发布属性定义不正确,或者此发布不存在。

  4. 调用 ValidatePublication 方法。 传递以下参数:

    这将把项目标记为等待验证。

  5. 如果尚未运行,则启动分发代理以同步每个订阅。 有关详细信息,请参阅如何同步推送订阅(RMO 编程)如何同步请求订阅(RMO 编程)。 验证操作的结果将写入代理历史记录。 有关详细信息,请参阅如何以编程方式监视复制(RMO 编程)

验证合并发布的所有订阅中的数据

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 MergePublication 类的实例。 设置发布的 NameDatabaseName 属性。 将 ConnectionContext 属性设置为步骤 1 中创建的连接。

  3. 调用 LoadProperties 方法获取该对象的其余属性。 如果此方法返回 false,则说明步骤 2 中的发布属性定义不正确,或者此发布不存在。

  4. 调用 ValidatePublication 方法。 传递所需的 ValidationOption

  5. 对每个订阅运行合并代理以开始验证,或者等待下一个计划的代理运行。 有关详细信息,请参阅如何同步请求订阅(RMO 编程)如何同步推送订阅(RMO 编程)。 验证操作的结果将写入代理的历史记录,您可以使用复制监视器查看。 有关详细信息,请参阅如何以编程方式监视复制(RMO 编程)

验证合并发布的单个订阅中的数据

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 MergePublication 类的实例。 设置发布的 NameDatabaseName 属性。 将 ConnectionContext 属性设置为步骤 1 中创建的连接。

  3. 调用 LoadProperties 方法获取该对象的其余属性。 如果此方法返回 false,则说明步骤 2 中的发布属性定义不正确,或者此发布不存在。

  4. 调用 ValidateSubscription 方法。 传递要验证的订阅服务器和订阅数据库的名称以及所需的 ValidationOption

  5. 对订阅运行合并代理以开始验证,或者等待下一个计划的代理运行。 有关详细信息,请参阅如何同步请求订阅(RMO 编程)如何同步推送订阅(RMO 编程)。 验证操作的结果将写入代理的历史记录,您可以使用复制监视器查看。 有关详细信息,请参阅如何以编程方式监视复制(RMO 编程)

示例

此示例将对事务发布的所有订阅标记为行计数验证。

         // Define the server, database, and publication names
            string publisherName = publisherInstance;
            string publicationName = "AdvWorksProductTran";
            string publicationDbName = "AdventureWorks";

            TransPublication publication;

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

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

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

                // If we can't get the properties for this publication, 
                // throw an application exception.
                if (publication.LoadProperties())
                {
                    // Initiate validataion for all subscriptions to this publication.
                    publication.ValidatePublication(ValidationOption.RowCountOnly,
                        ValidationMethod.ConditionalFast, false);

                    // If not already running, start the Distribution Agent at each 
                    // Subscriber to synchronize and validate the subscriptions.
                }
                else
                {
                    throw new ApplicationException(String.Format(
                        "Settings could not be retrieved for the publication. " +
                        "Ensure that the publication {0} exists on {1}.",
                        publicationName, publisherName));
                }
            }
            catch (Exception ex)
            {
                // Do error handling here.
                throw new ApplicationException(
                    "Subscription validation could not be initiated.", ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Define the server, database, and publication names
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks"

Dim publication As TransPublication

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

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

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

    ' If we can't get the properties for this publication, 
    ' throw an application exception.
    If publication.LoadProperties() Then

        ' Initiate validataion for all subscriptions to this publication.
        publication.ValidatePublication(ValidationOption.RowCountOnly, _
         ValidationMethod.ConditionalFast, False)

        ' If not already running, start the Distribution Agent at each 
        ' Subscriber to synchronize and validate the subscriptions.
    Else
        Throw New ApplicationException(String.Format( _
         "Settings could not be retrieved for the publication. " + _
         "Ensure that the publication {0} exists on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Do error handling here.
    Throw New ApplicationException( _
     "Subscription validation could not be initiated.", ex)
Finally
    conn.Disconnect()
End Try

此示例将对合并发布的特定订阅标记为行计数验证。

         // Define the server, database, and publication names
            string publisherName = publisherInstance;
            string publicationName = "AdvWorksSalesOrdersMerge";
            string publicationDbName = "AdventureWorks";
            string subscriberName = subscriberInstance;
            string subscriptionDbName = "AdventureWorksReplica";

            MergePublication publication;

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

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

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


                // If we can't get the properties for this merge publication, then throw an application exception.
                if (publication.LoadProperties())
                {
                    // Initiate validation of the specified subscription.
                    publication.ValidateSubscription(subscriberName,
                        subscriptionDbName, ValidationOption.RowCountOnly);
                    
                    // Start the Merge Agent to synchronize and validate the subscription.
                }
                else
                {
                    throw new ApplicationException(String.Format(
                        "Settings could not be retrieved for the publication. " +
                        "Ensure that the publication {0} exists on {1}.",
                        publicationName, publisherName));
                }
            }
            catch (Exception ex)
            {
                // Do error handling here.
                throw new ApplicationException(String.Format(
                    "The subscription at {0} to the {1} publication could not " +
                    "be validated.", subscriberName, publicationName), ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Define the server, database, and publication names
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks"
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorksReplica"

Dim publication As MergePublication

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

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

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

    ' If we can't get the properties for this merge publication, then throw an application exception.
    If publication.LoadProperties() Then
        ' Initiate validation of the specified subscription.
        publication.ValidateSubscription(subscriberName, _
         subscriptionDbName, ValidationOption.RowCountOnly)

        ' Start the Merge Agent to synchronize and validate the subscription.
    Else
        Throw New ApplicationException(String.Format( _
         "Settings could not be retrieved for the publication. " + _
         "Ensure that the publication {0} exists on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Do error handling here.
    Throw New ApplicationException(String.Format( _
     "The subscription at {0} to the {1} publication could not " + _
     "be validated.", subscriberName, publicationName), ex)
Finally
    conn.Disconnect()
End Try