如何:檢視及修改發行集屬性 (RMO 程式設計)
您可以使用「複寫管理物件」(RMO) 以程式設計的方式修改發行集及存取其屬性。用來檢視或修改發行集屬性的 RMO 類別,將取決於發行集的類型而定。
檢視或修改快照式或交易式發行集的屬性
使用 ServerConnection 類別建立與發行者的連接。
建立 TransPublication 類別的執行個體、為發行集設定 Name 和 DatabaseName 屬性,以及將 ConnectionContext 屬性設定為步驟 1 中所建立的連接。
呼叫 LoadProperties 方法以取得物件的屬性。如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。
(選擇性) 若要變更屬性,請針對一或多個可設定的屬性設定新的值。使用邏輯 AND 運算子 (Microsoft Visual C# 中的 & 及 Microsoft Visual Basic 中的 And),以判斷是否已針對 Attributes 屬性設定給定的 PublicationAttributes 值。使用包含的邏輯 OR 運算子 (Visual C# 中的 | 和 Visual Basic 中的 Or),並使用排除的邏輯 OR 運算子 (Visual C# 中的 ^ 和 Visual Basic 中的 Xor),為 Attributes 屬性變更 PublicationAttributes 值。
(選擇性) 如果您已針對 CachePropertyChanges 指定 true 的值,請呼叫 CommitPropertyChanges 方法來認可伺服器上的變更。如果您已針對 CachePropertyChanges 指定 false 的值 (預設值),則會立即將變更傳送到伺服器。
檢視或修改合併式發行集的屬性
使用 ServerConnection 類別建立與發行者的連接。
建立 MergePublication 類別的執行個體、為發行集設定 Name 和 DatabaseName 屬性,以及將 ConnectionContext 屬性設定為步驟 1 中所建立的連接。
呼叫 LoadProperties 方法以取得物件的屬性。如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。
(選擇性) 若要變更屬性,請針對一或多個可設定的屬性設定新的值。使用邏輯 AND 運算子 (Visual C# 中的 & 及 Visual Basic 中的 And),以判斷是否已針對 Attributes 屬性設定給定的 PublicationAttributes 值。使用包含的邏輯 OR 運算子 (Visual C# 中的 | 和 Visual Basic 中的 Or),並使用排除的邏輯 OR 運算子 (Visual C# 中的 ^ 和 Visual Basic 中的 Xor),為 Attributes 屬性變更 PublicationAttributes 值。
(選擇性) 如果您已針對 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