Share via


パブリケーション プロパティの表示と変更の方法 (RMO プログラミング)

レプリケーション管理オブジェクト (RMO) を使用して、パブリケーションの変更とそのプロパティへのアクセスをプログラムから実行できます。パブリケーション プロパティの表示と変更に使用する RMO クラスは、パブリケーションの種類によって異なります。

スナップショット パブリケーションまたはトランザクション パブリケーションのプロパティを表示または変更するには

  1. ServerConnection クラスを使用して、パブリッシャへの接続を作成します。

  2. TransPublication クラスのインスタンスを作成し、パブリケーションの Name プロパティと DatabaseName プロパティを設定して、手順 1. で作成した接続を ConnectionContext プロパティに設定します。

  3. LoadProperties メソッドを呼び出して、オブジェクトのプロパティを取得します。このメソッドが false を返す場合、手順 2. でパブリケーション プロパティを不適切に設定したか、パブリケーションが存在していません。

  4. (省略可) プロパティを変更するには、設定可能なプロパティに新しい値を設定します。Attributes プロパティに特定の PublicationAttributes 値が設定されているかどうかを判断するには、論理積演算子 (Microsoft Visual C# では &、Microsoft Visual Basic では And) を使用します。Attributes プロパティの PublicationAttributes 値を変更するには、包括的論理和演算子 (Visual C# では |、Visual Basic では Or) および排他的論理和演算子 (Visual C# では ^、Visual Basic では Xor) を使用します。

  5. (省略可) CachePropertyChanges に true を指定した場合、CommitPropertyChanges メソッドを呼び出してサーバーに変更をコミットします。CachePropertyChanges に false (既定値) を指定した場合、変更は直ちにサーバーに送られます。

マージ パブリケーションのプロパティを表示または変更するには

  1. ServerConnection クラスを使用して、パブリッシャへの接続を作成します。

  2. MergePublication クラスのインスタンスを作成し、パブリケーションの Name プロパティと DatabaseName プロパティを設定して、手順 1. で作成した接続を ConnectionContext プロパティに設定します。

  3. LoadProperties メソッドを呼び出して、オブジェクトのプロパティを取得します。このメソッドが false を返す場合、手順 2. でパブリケーション プロパティを不適切に設定したか、パブリケーションが存在していません。

  4. (省略可) プロパティを変更するには、設定可能なプロパティに新しい値を設定します。Attributes プロパティに特定の PublicationAttributes 値が設定されているかどうかを判断するには、論理積演算子 (Visual C# では &、Visual Basic では And) を使用します。Attributes プロパティの PublicationAttributes 値を変更するには、包括的論理和演算子 (Visual C# では |、Visual Basic では Or) および排他的論理和演算子 (Visual C# では ^、Visual Basic では Xor) を使用します。

  5. (省略可) CachePropertyChanges に true を指定した場合、CommitPropertyChanges メソッドを呼び出してサーバーに変更をコミットします。CachePropertyChanges に false (既定値) を指定した場合、変更は直ちにサーバーに送られます。

使用例

次の例では、トランザクション パブリケーションにパブリケーション属性を設定します。変更は明示的にサーバーに送られるまでキャッシュされます。

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

            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;

                // Explicitly enable caching of property changes on this object.
                publication.CachePropertyChanges = true;

                // If we can't get the properties for this publication, 
                // throw an application exception.
                if (publication.LoadProperties())
                {
                    // Enable support for push subscriptions and disable support 
                    // for pull subscriptions.
                    if ((publication.Attributes & PublicationAttributes.AllowPull) != 0)
                    {
                        publication.Attributes ^= PublicationAttributes.AllowPull;
                    }
                    if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
                    {
                        publication.Attributes |= PublicationAttributes.AllowPush;
                    }

                    // Send changes to the server.
                    publication.CommitPropertyChanges();
                }
                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(
                    "The publication property could not be changed.", 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 = "AdventureWorks2008R2"

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

    ' Explicitly enable caching of property changes on this object.
    publication.CachePropertyChanges = True

    ' If we can't get the properties for this publication, 
    ' throw an application exception.
    If publication.LoadProperties() Then
        ' Enable support for push subscriptions and disable support 
        ' for pull subscriptions.
        If (publication.Attributes And PublicationAttributes.AllowPull) <> 0 Then
            publication.Attributes = publication.Attributes _
            Xor PublicationAttributes.AllowPull
        End If
        If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
            publication.Attributes = publication.Attributes _
            Or PublicationAttributes.AllowPush
        End If

        ' Send changes to the server.
        publication.CommitPropertyChanges()
    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( _
        "The publication property could not be changed.", ex)
Finally
    conn.Disconnect()
End Try

次の例では、マージ パブリケーションの DDL レプリケーションを無効にします。

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

            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())
                {
                    // If DDL replication is currently enabled, disable it.
                    if (publication.ReplicateDdl == DdlReplicationOptions.All)
                    {
                        publication.ReplicateDdl = DdlReplicationOptions.None;
                    }
                    else
                    {
                        publication.ReplicateDdl = DdlReplicationOptions.All;
                    }
                }
                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(
                    "The publication property could not be changed.", 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 = "AdventureWorks2008R2"

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
        ' If DDL replication is currently enabled, disable it.
        If publication.ReplicateDdl = DdlReplicationOptions.All Then
            publication.ReplicateDdl = DdlReplicationOptions.None
        Else
            publication.ReplicateDdl = DdlReplicationOptions.All
        End If
    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( _
        "The publication property could not be changed.", ex)
Finally
    conn.Disconnect()
End Try