语言

如何查看和修改发布属性(RMO 编程)

可使用复制管理对象 (RMO) 以编程方式修改发布和访问其属性。 查看或修改发布属性所使用的 RMO 类取决于发布的类型。

查看或修改快照发布或事务发布的属性

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 TransPublication 类的实例,为发布设置 Name 和 DatabaseName 属性并将 ConnectionContext 属性设置为步骤 1 中创建的连接。

  3. 调用 LoadProperties 方法以获取对象的属性。 如果此方法返回 false,则表示步骤 2 中定义的发布属性不正确,或者表示该发布不存在。

  4. (可选)若要更改属性,为一个或多个可设置的属性设置新值。 使用逻辑 AND 运算符(在 Microsoft Visual C# 中为 &,在 Microsoft Visual Basic 中为 And)可确定是否为 Attributes 属性设置了给定的 PublicationAttributes 值。 使用逻辑 OR 运算符(在 Visual C# 中为 |,在 Visual Basic 中为 Or)和逻辑异或运算符(在 Visual C# 中为 ^,在 Visual Basic 中为 Xor)可更改 Attributes 属性的 PublicationAttributes 值。

  5. (可选)如果已将 CachePropertyChanges 的值指定为 true,则调用 CommitPropertyChanges 方法以在服务器上提交更改。 如果已将 CachePropertyChanges 的值指定为 false(默认值),则会立即将更改发送到服务器。

查看或修改合并发布的属性

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 MergePublication 类的实例,为发布设置 Name 和 DatabaseName 属性并将 ConnectionContext 属性设置为在步骤 1 中创建的连接。

  3. 调用 LoadProperties 方法以获取对象的属性。 如果此方法返回 false,则表示步骤 2 中定义的发布属性不正确,或者表示该发布不存在。

  4. (可选)若要更改属性,为一个或多个可设置的属性设置新值。 使用逻辑 AND 运算符(在 Visual C# 中为 &,在 Visual Basic 中为 And)可确定是否为 Attributes 属性设置了给定的 PublicationAttributes 值。 使用逻辑 OR 运算符(在 Visual C# 中为 |,在 Visual Basic 中为 Or)和逻辑异或运算符(在 Visual C# 中为 ^,在 Visual Basic 中为 Xor)可更改 Attributes 属性的 PublicationAttributes 值。

  5. (可选)如果已将 CachePropertyChanges 的值指定为 true,则调用 CommitPropertyChanges 方法以在服务器上提交更改。 如果已将 CachePropertyChanges 的值指定为 false(默认值),则会立即将更改发送到服务器。

示例

该示例设置事务发布的发布属性。 这些更改将被缓存,直至将其显式发送到服务器。

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

            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 = "AdventureWorks"

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 = "AdventureWorks";

            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 = "AdventureWorks"

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