Vorgehensweise: Anzeigen und Ändern von Veröffentlichungseigenschaften (RMO-Programmierung)

Sie können Veröffentlichungen ändern und mithilfe von Replikationsverwaltungsobjekten (RMO) programmgesteuert auf ihre Eigenschaften zugreifen. Welche RMO-Klassen zum Anzeigen oder Ändern von Veröffentlichungseigenschaften verwendet werden, hängt vom Typ der Veröffentlichung ab.

So zeigen Sie die Eigenschaften eines Snapshots oder einer Transaktionsveröffentlichung an oder ändern diese

  1. Erstellen Sie eine Verbindung mit dem Verleger, indem Sie die ServerConnection-Klasse verwenden.

  2. Erstellen Sie eine Instanz der TransPublication-Klasse, legen Sie die Name-Eigenschaft und die DatabaseName-Eigenschaft für die Veröffentlichung fest, und legen Sie die ConnectionContext-Eigenschaft auf die in Schritt 1 erstellte Verbindung fest.

  3. Rufen Sie die LoadProperties-Methode auf, um die Eigenschaften des Objekts abzurufen. Wenn diese Methode false zurückgibt, sind die Veröffentlichungseigenschaften in Schritt 2 falsch definiert, oder die Veröffentlichung ist nicht vorhanden.

  4. (Optional) Um Eigenschaften zu ändern, legen Sie einen neuen Wert für eine oder mehrere festlegbaren Eigenschaften fest. Verwenden Sie den logischen AND-Operator (& in Microsoft Visual C# and And in Microsoft Visual Basic), um zu ermitteln, ob ein gegebener PublicationAttributes-Wert für die Attributes-Eigenschaft festgelegt wurde. Verwenden Sie den inklusiven logischen OR-Operator (| in Visual C# and Or in Visual Basic) und den exklusiven logischen OR-Operator (^ in Visual C# und Xor in Visual Basic), um die PublicationAttributes-Werte für die Attributes-Eigenschaft zu ändern.

  5. (Optional) Wenn Sie den Wert true für CachePropertyChanges angegeben haben, rufen Sie die CommitPropertyChanges-Methode auf, um die Änderungen auf dem Server einzutragen. Wenn Sie den Wert false für CachePropertyChanges (die Standardeinstellung) angegeben haben, werden die Änderungen sofort an den Server gesendet.

So zeigen Sie die Eigenschaften einer Mergeveröffentlichung an oder ändern diese

  1. Erstellen Sie eine Verbindung mit dem Verleger, indem Sie die ServerConnection-Klasse verwenden.

  2. Erstellen Sie eine Instanz der MergePublication-Klasse, legen Sie die Name-Eigenschaft und die DatabaseName-Eigenschaft der Veröffentlichung fest, und legen Sie die ConnectionContext-Eigenschaft auf die in Schritt 1 erstellte Verbindung fest.

  3. Rufen Sie die LoadProperties-Methode auf, um die Eigenschaften des Objekts abzurufen. Wenn diese Methode false zurückgibt, sind die Veröffentlichungseigenschaften in Schritt 2 falsch definiert, oder die Veröffentlichung ist nicht vorhanden.

  4. (Optional) Um Eigenschaften zu ändern, legen Sie einen neuen Wert für eine oder mehrere festlegbaren Eigenschaften fest. Verwenden Sie den logischen AND-Operator (& in Microsoft Visual C# and And in Microsoft Visual Basic), um zu ermitteln, ob ein gegebener PublicationAttributes-Wert für die Attributes-Eigenschaft festgelegt wurde. Verwenden Sie den inklusiven logischen OR-Operator (| in Visual C# and Or in Visual Basic) und den exklusiven logischen OR-Operator (^ in Visual C# und Xor in Visual Basic), um die PublicationAttributes-Werte für die Attributes-Eigenschaft zu ändern.

  5. (Optional) Wenn Sie den Wert true für CachePropertyChanges angegeben haben, rufen Sie die CommitPropertyChanges-Methode auf, um die Änderungen auf dem Server einzutragen. Wenn Sie den Wert false für CachePropertyChanges (die Standardeinstellung) angegeben haben, werden die Änderungen sofort an den Server gesendet.

Beispiel

In diesem Beispiel werden Veröffentlichungsattribute einer Transaktionsveröffentlichung festgelegt. Die Änderungen werden zwischengespeichert, bis sie explizit zum Server gesendet werden.

            // 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

In diesem Beispiel wird die DDL-Replikation für eine Mergeveröffentlichung deaktiviert.

         // 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