共用方式為


如何:建立發送訂閱 (RMO 程式設計)

您可以使用「複寫管理物件」(RMO) 以程式設計的方式建立發送訂閱。用於建立發送訂閱的 RMO 類別依該訂閱所屬的發行集類型而定。

安全性注意事項安全性注意事項

可能的話,會在執行階段提示使用者輸入安全性認證。如果您必須儲存認證,請使用 Microsoft Windows .NET Framework 提供的密碼編譯服務

若要建立快照式或交易式發行集的發送訂閱

  1. 使用 ServerConnection 類別建立到「發行者」的連接。

  2. 使用步驟 1 中的「發行者」連接建立 TransPublication 類別的執行個體。指定 NameDatabaseNameConnectionContext

  3. 呼叫 LoadProperties 方法。如果此方法傳回 false,則表示步驟 2 中指定的屬性不正確,或伺服器上不存在該發行集。

  4. Attributes 屬性和 AllowPush 之間執行位元運算邏輯 AND (Visual C# 中的 & 和 Visual Basic 中的 And)。如果結果為 None,則將 Attributes 設為 AttributesAllowPush 之間位元運算邏輯 OR (Visual C# 中的 | 和 Visual Basic 中的 Or) 的結果。然後呼叫 CommitPropertyChanges 以啟用發送訂閱。

  5. 如果訂閱資料庫不存在,可使用 Database 類別建立它。如需詳細資訊,請參閱<建立、改變和移除資料庫>。

  6. 建立 TransSubscription 類別的執行個體。

  7. 設定下列訂閱屬性:

  8. 呼叫 Create 方法。

    安全性注意事項安全性注意事項

    當利用遠端「散發者」來設定「發行者」時,提供給所有屬性的值 (包括 SynchronizationAgentProcessSecurity) 都會以純文字的方式傳給「散發者」。您應該先加密「發行者」及其遠端「散發者」之間的連接,再呼叫 Create 方法。如需詳細資訊,請參閱<加密 SQL Server 的連接>。

若要建立合併式發行集的發送訂閱

  1. 使用 ServerConnection 類別建立到「發行者」的連接。

  2. 使用步驟 1 中的「發行者」連接建立 MergePublication 類別的執行個體。指定 NameDatabaseNameConnectionContext

  3. 呼叫 LoadProperties 方法。如果此方法傳回 false,則表示步驟 2 中指定的屬性不正確,或伺服器上不存在該發行集。

  4. Attributes 屬性和 AllowPush 之間執行位元運算邏輯 AND (Visual C# 中的 & 和 Visual Basic 中的 And)。如果結果為 None,則將 Attributes 設為 AttributesAllowPush 之間位元運算邏輯 OR (Visual C# 中的 | 和 Visual Basic 中的 Or) 的結果。然後呼叫 CommitPropertyChanges 以啟用發送訂閱。

  5. 如果訂閱資料庫不存在,可使用 Database 類別建立它。如需詳細資訊,請參閱<建立、改變和移除資料庫>。

  6. 建立 MergeSubscription 類別的執行個體。

  7. 設定下列訂閱屬性:

  8. 呼叫 Create 方法。

    安全性注意事項安全性注意事項

     利用遠端「散發者」來建立「發行者」上的發送訂閱時,提供給所有屬性的值 (包括 SynchronizationAgentProcessSecurity) 都會以純文字的方式傳給「散發者」。您應該先加密「發行者」及其遠端「散發者」之間的連接,再呼叫 Create 方法。如需詳細資訊,請參閱<加密 SQL Server 的連接>。

範例

此範例會建立交易式發行集的新發送訂閱。用於執行「散發代理程式」作業的 Windows 帳戶認證是在執行階段傳遞的。

         // Define the Publisher, publication, and databases.
            string publicationName = "AdvWorksProductTran";
            string publisherName = publisherInstance;
            string subscriberName = subscriberInstance;
            string subscriptionDbName = "AdventureWorksReplica";
            string publicationDbName = "AdventureWorks";

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

            // Create the objects that we need.
            TransPublication publication;
            TransSubscription subscription;

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

                // Ensure that the publication exists and that 
                // it supports push subscriptions.
                publication = new TransPublication();
                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 TransSubscription();
                    subscription.ConnectionContext = conn;
                    subscription.SubscriberName = subscriberName;
                    subscription.PublicationName = publicationName;
                    subscription.DatabaseName = publicationDbName;
                    subscription.SubscriptionDBName = subscriptionDbName;

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

                    // By default, subscriptions to transactional publications are synchronized 
                    // continuously, but in this case we only want to synchronize on demand.
                    subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.OnDemand;

                    // 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 = "AdvWorksProductTran"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"

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

' Create the objects that we need.
Dim publication As TransPublication
Dim subscription As TransSubscription

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

    ' Ensure that the publication exists and that 
    ' it supports push subscriptions.
    publication = New TransPublication()
    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 TransSubscription()
        subscription.ConnectionContext = conn
        subscription.SubscriberName = subscriberName
        subscription.PublicationName = publicationName
        subscription.DatabaseName = publicationDbName
        subscription.SubscriptionDBName = subscriptionDbName

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

        ' By default, subscriptions to transactional publications are synchronized 
        ' continuously, but in this case we only want to synchronize on demand.
        subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.OnDemand

        ' 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

此範例會建立合併式發行集的新發送訂閱。用於執行「合併代理程式」作業的 Windows 帳戶認證是在執行階段傳遞的。

           // Define the Publisher, publication, and databases.
            string publicationName = "AdvWorksSalesOrdersMerge";
            string publisherName = publisherInstance;
            string subscriberName = subscriberInstance;
            string subscriptionDbName = "AdventureWorksReplica";
            string publicationDbName = "AdventureWorks";
            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 = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"
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