如何:檢視和修改發行者和散發者屬性 (RMO 程式設計)

您可以使用「複寫管理物件」(RMO) 以程式設計的方式檢視和修改發行者和散發者的屬性。

檢視和修改散發者屬性

  1. 使用 ServerConnection 類別建立與散發者的連接。

  2. 建立 ReplicationServer 類別的執行個體。傳遞步驟 1 的 ServerConnection 物件。

  3. (選擇性) 檢查 IsDistributor 屬性,確認目前連接的伺服器為散發者。

  4. 呼叫 Load 方法,從伺服器中取得屬性。

  5. (選擇性) 若要變更屬性,請針對 ReplicationServer 物件上可以設定的一或多個散發者屬性設定新的值。

  6. (選擇性) 如果 ReplicationServer 物件上的 CachePropertyChanges 屬性設定為 true,請呼叫 CommitPropertyChanges 方法來認可伺服器的變更。

檢視及修改散發資料庫屬性

  1. 使用 ServerConnection 類別建立與散發者的連接。

  2. 建立 DistributionDatabase 類別的執行個體。指定 name 屬性,並傳遞步驟 1 中的 ServerConnection 物件。

  3. 呼叫 LoadProperties 方法,從伺服器中取得屬性。如果此方法傳回 false,則表示伺服器上沒有指定之名稱的資料庫存在。

  4. (選擇性) 若要變更屬性,請針對其中一個可設定的 DistributionDatabase 屬性設定新的值。

  5. (選擇性) 如果 DistributionDatabase 物件上的 CachePropertyChanges 屬性設定為 true,請呼叫 CommitPropertyChanges 方法來認可伺服器的變更。

檢視和修改發行者屬性

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

  2. 建立 DistributionPublisher 類別的執行個體。指定 Name 屬性,並傳遞步驟 1 中的 ServerConnection 物件。

  3. (選擇性) 若要變更屬性,請針對其中一個可設定的 DistributionPublisher 屬性設定新的值。

  4. (選擇性) 如果 DistributionPublisher 物件上的 CachePropertyChanges 屬性設定為 true,請呼叫 CommitPropertyChanges 方法來認可伺服器的變更。

變更從發行者到散發者之管理連接的密碼

  1. 使用 ServerConnection 類別建立與散發者的連接。

  2. 建立 ReplicationServer 類別的執行個體。

  3. ConnectionContext 屬性設定為在步驟 1 中建立的連接。

  4. 呼叫 Load 方法以取得物件的屬性。

  5. 呼叫 ChangeDistributorPassword 方法。針對 password 參數傳遞新的密碼值。

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

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

  6. (選擇性) 請執行以下步驟,在使用此散發者的每一個遠端發行者上變更密碼:

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

    2. 建立 ReplicationServer 類別的執行個體。

    3. ConnectionContext 屬性設定為在步驟 6a 中建立的連接。

    4. 呼叫 Load 方法以取得物件的屬性。

    5. 呼叫 ChangeDistributorPassword 方法。針對 password 參數傳遞步驟 5 中的新密碼值。

範例

這個範例會示範如何變更散發和散發資料庫的屬性。

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

為了避免在程式碼中儲存認證,會在執行階段提供新的散發者密碼。

           // Set the Distributor and distribution database names.
            string distributionDbName = "distribution";
            string distributorName = publisherInstance;

            ReplicationServer distributor;
            DistributionDatabase distributionDb;

            // Create a connection to the Distributor using Windows Authentication.
            ServerConnection conn = new ServerConnection(distributorName);

            try
            {
                // Open the connection. 
                conn.Connect();

                distributor = new ReplicationServer(conn);

                // Load Distributor properties, if it is installed.
                if (distributor.LoadProperties())
                {
                    // Password supplied at runtime.
                    distributor.ChangeDistributorPassword(password);
                    distributor.AgentCheckupInterval = 5;

                    // Save changes to the Distributor properties.
                    distributor.CommitPropertyChanges();
                }
                else
                {
                    throw new ApplicationException(
                        String.Format("{0} is not a Distributor.", publisherInstance));
                }

                // Create an object for the distribution database 
                // using the open Distributor connection.
                distributionDb = new DistributionDatabase(distributionDbName, conn);

                // Change distribution database properties.
                if (distributionDb.LoadProperties())
                {
                    // Change maximum retention period to 48 hours and history retention 
                    // period to 24 hours.
                    distributionDb.MaxDistributionRetention = 48;
                    distributionDb.HistoryRetention = 24;

                    // Save changes to the distribution database properties.
                    distributionDb.CommitPropertyChanges();
                }
                else
                {
                    // Do something here if the distribution database does not exist.
                }
            }
            catch (Exception ex)
            {
                // Implement the appropriate error handling here. 
                throw new ApplicationException("An error occured when changing Distributor " +
                    " or distribution database properties.", ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Set the Distributor and distribution database names.
Dim distributionDbName As String = "distribution"
Dim distributorName As String = publisherInstance

Dim distributor As ReplicationServer
Dim distributionDb As DistributionDatabase

' Create a connection to the Distributor using Windows Authentication.
Dim conn As ServerConnection = New ServerConnection(distributorName)

Try
    ' Open the connection. 
    conn.Connect()

    distributor = New ReplicationServer(conn)

    ' Load Distributor properties, if it is installed.
    If distributor.LoadProperties() Then
        ' Password supplied at runtime.
        distributor.ChangeDistributorPassword(password)
        distributor.AgentCheckupInterval = 5

        ' Save changes to the Distributor properties.
        distributor.CommitPropertyChanges()
    Else
        Throw New ApplicationException( _
            String.Format("{0} is not a Distributor.", publisherInstance))
    End If

    ' Create an object for the distribution database 
    ' using the open Distributor connection.
    distributionDb = New DistributionDatabase(distributionDbName, conn)

    ' Change distribution database properties.
    If distributionDb.LoadProperties() Then
        ' Change maximum retention period to 48 hours and history retention 
        ' period to 24 hours.
        distributionDb.MaxDistributionRetention = 48
        distributionDb.HistoryRetention = 24

        ' Save changes to the distribution database properties.
        distributionDb.CommitPropertyChanges()
    Else
        ' Do something here if the distribution database does not exist.
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here. 
    Throw New ApplicationException("An error occured when changing Distributor " + _
        " or distribution database properties.", ex)
Finally
    conn.Disconnect()
End Try