如何指定同步计划(RMO 编程)

复制使用 SQL Server 代理来为定期发生的活动计划作业(如快照生成和订阅同步)。 可以编程的方式使用复制管理对象 (RMO) 为复制代理作业指定计划。

注意注意

创建一个订阅并将 CreateSyncAgentByDefault(请求订阅的默认行为)的值指定为 false 时,不会创建代理作业,并忽略计划属性。 在这种情况下,必须由应用程序确定同步计划。 有关详细信息,请参阅如何创建请求订阅(RMO 编程)如何创建推送订阅(RMO 编程)

在创建事务发布的推送订阅时定义一个复制代理计划

  1. 为要创建的订阅创建一个 TransSubscription 类的实例。 有关详细信息,请参阅如何创建推送订阅(RMO 编程)

  2. 在调用 Create 之前,设置 AgentSchedule 属性的下列一个或多个字段:

    注意注意

    如果未指定上述某个属性,将设置为默认值。

  3. 调用 Create 方法创建订阅。

在创建事务发布的请求订阅时定义一个复制代理计划

  1. 为要创建的订阅创建一个 TransPullSubscription 类的实例。 有关详细信息,请参阅如何创建请求订阅(RMO 编程)

  2. 在调用 Create 之前,设置 AgentSchedule 属性的下列一个或多个字段:

    注意注意

    如果未指定上述某个属性,将设置为默认值。

  3. 调用 Create 方法创建订阅。

在创建合并发布的请求订阅时定义一个复制代理计划

  1. 为要创建的订阅创建一个 MergePullSubscription 类的实例。 有关详细信息,请参阅如何创建请求订阅(RMO 编程)

  2. 在调用 Create 之前,设置 AgentSchedule 属性的下列一个或多个字段:

    注意注意

    如果未指定上述某个属性,将设置为默认值。

  3. 调用 Create 方法创建订阅。

在创建合并发布的推送订阅时定义一个复制代理计划

  1. 为要创建的订阅创建一个 MergeSubscription 类的实例。 有关详细信息,请参阅如何创建推送订阅(RMO 编程)

  2. 在调用 Create 之前,设置 AgentSchedule 属性的下列一个或多个字段:

    注意注意

    如果未指定上述某个属性,将设置为默认值。

  3. 调用 Create 方法创建订阅。

示例

此示例创建一个合并发布的推送订阅并指定同步订阅计划。

          // Define the Publisher, publication, and databases.
            string publicationName = "AdvWorksSalesOrdersMerge";
            string publisherName = publisherInstance;
            string subscriberName = subscriberInstance;
            string subscriptionDbName = "AdventureWorks2008R2Replica";
            string publicationDbName = "AdventureWorks2008R2";
            string hostname = @"adventure-works\garrett1";

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

            // Create the objects that we need.
            MergePublication publication;
            MergeSubscription subscription;

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

                // Ensure that the publication exists and that 
                // it supports push subscriptions.
                publication = new MergePublication();
                publication.Name = publicationName;
                publication.DatabaseName = publicationDbName;
                publication.ConnectionContext = conn;

                if (publication.IsExistingObject)
                {
                    if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
                    {
                        publication.Attributes |= PublicationAttributes.AllowPush;
                    }

                    // Define the push subscription.
                    subscription = new MergeSubscription();
                    subscription.ConnectionContext = conn;
                    subscription.SubscriberName = subscriberName;
                    subscription.PublicationName = publicationName;
                    subscription.DatabaseName = publicationDbName;
                    subscription.SubscriptionDBName = subscriptionDbName;
                    subscription.HostName = hostname;

                    // Set a schedule to synchronize the subscription every 2 hours
                    // during weekdays from 6am to 10pm.
                    subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly;
                    subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32(0x003E);
                    subscription.AgentSchedule.FrequencyRecurrenceFactor = 1;
                    subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour;
                    subscription.AgentSchedule.FrequencySubDayInterval = 2;
                    subscription.AgentSchedule.ActiveStartDate = 20051108;
                    subscription.AgentSchedule.ActiveEndDate = 20071231;
                    subscription.AgentSchedule.ActiveStartTime = 060000;
                    subscription.AgentSchedule.ActiveEndTime = 100000;

                    // Specify the Windows login credentials for the Merge Agent job.
                    subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
                    subscription.SynchronizationAgentProcessSecurity.Password = winPassword;

                    // Create the push subscription.
                    subscription.Create();
                }
                else
                {
                    // Do something here if the publication does not exist.
                    throw new ApplicationException(String.Format(
                        "The publication '{0}' does not exist on {1}.",
                        publicationName, publisherName));
                }
            }
            catch (Exception ex)
            {
                // Implement the appropriate error handling here.
                throw new ApplicationException(String.Format(
                    "The subscription to {0} could not be created.", publicationName), ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2008R2Replica"
Dim publicationDbName As String = "AdventureWorks2008R2"
Dim hostname As String = "adventure-works\garrett1"

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

' Create the objects that we need.
Dim publication As MergePublication
Dim subscription As MergeSubscription

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

    ' Ensure that the publication exists and that 
    ' it supports push subscriptions.
    publication = New MergePublication()
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName
    publication.ConnectionContext = conn

    If publication.IsExistingObject Then
        If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
            publication.Attributes = publication.Attributes _
            Or PublicationAttributes.AllowPush
        End If

        ' Define the push subscription.
        subscription = New MergeSubscription()
        subscription.ConnectionContext = conn
        subscription.SubscriberName = subscriberName
        subscription.PublicationName = publicationName
        subscription.DatabaseName = publicationDbName
        subscription.SubscriptionDBName = subscriptionDbName
        subscription.HostName = hostname

        ' Set a schedule to synchronize the subscription every 2 hours
        ' during weekdays from 6am to 10pm.
        subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly
        subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32("0x003E", 16)
        subscription.AgentSchedule.FrequencyRecurrenceFactor = 1
        subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour
        subscription.AgentSchedule.FrequencySubDayInterval = 2
        subscription.AgentSchedule.ActiveStartDate = 20051108
        subscription.AgentSchedule.ActiveEndDate = 20071231
        subscription.AgentSchedule.ActiveStartTime = 60000
        subscription.AgentSchedule.ActiveEndTime = 100000

        ' Specify the Windows login credentials for the Merge Agent job.
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword

        ' Create the push subscription.
        subscription.Create()
    Else

        ' Do something here if the publication does not exist.
        Throw New ApplicationException(String.Format( _
         "The publication '{0}' does not exist on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here.
    Throw New ApplicationException(String.Format( _
    "The subscription to {0} could not be created.", publicationName), ex)
Finally
    conn.Disconnect()
End Try