如何:建立發行集 (RMO 程式設計)
您可以使用「複寫管理物件」(RMO) 以程式設計的方式建立發行集。用來建立發行集的 RMO 類別,將取決於所建立的發行集類型而定。
安全性注意事項 |
---|
可能的話,會在執行階段提示使用者輸入安全性認證。如果您必須儲存認證,請使用 Microsoft Windows .NET Framework 提供的<密碼編譯服務>(英文)。 |
建立快照式或交易式發行集
使用 ServerConnection 類別建立與發行者的連接。
為發行集資料庫建立 ReplicationDatabase 類別的執行個體、將 ConnectionContext 屬性設定為步驟 1 中的 ServerConnection 執行個體,並呼叫 LoadProperties 方法。如果 LoadProperties 傳回 false,請確認此資料庫確實存在。
如果 EnabledTransPublishing 屬性為 false,請將它設定為 true。
如果是交易式發行集,請檢查 LogReaderAgentExists 屬性的值。如果這個屬性為 true,則表示記錄讀取器代理程式作業已存在此資料庫中。如果這個屬性為 false,請執行下列動作:
設定 LogReaderAgentProcessSecurity 的 Login 和 Password 或 SecurePassword 欄位,以提供執行記錄讀取器代理程式所使用之 Microsoft Windows 帳戶的認證。
[!附註]
當發行集是由 sysadmin 固定伺服器角色的成員所建立時,不需要設定 LogReaderAgentProcessSecurity。在這種情況下,代理程式會模擬「SQL Server 」帳戶。如需詳細資訊,請參閱<複寫代理程式安全性模型>。
(選擇性) 在使用「SQL Server 驗證」連接到發行者時,請設定 LogReaderAgentPublisherSecurity 的 SqlStandardLogin 和 SqlStandardPassword (或 SecureSqlStandardPassword) 欄位。
呼叫 CreateLogReaderAgent 方法來為資料庫建立記錄讀取器代理程式作業。
建立 TransPublication 類別的執行個體,並為此物件設定下列屬性:
將 ConnectionContext 設定為步驟 1 中的 ServerConnection。
將 DatabaseName 設為發行的資料庫名稱。
將 Name 設定為發行集名稱。
SnapshotGenerationAgentProcessSecurity 的 Login 和 Password 欄位,可提供執行快照集代理程式所使用之 Windows 帳戶的認證。當快照集代理程式要連接本機散發者及進行任何遠端連接 (使用 Windows 驗證) 時,也會使用此帳戶。
[!附註]
當發行集是由 sysadmin 固定伺服器角色的成員所建立時,不需要設定 SnapshotGenerationAgentProcessSecurity。在這種情況下,代理程式會模擬「SQL Server 」帳戶。如需詳細資訊,請參閱<複寫代理程式安全性模型>。
(選擇性) 在使用「SQL Server 驗證」連接到發行者時,請設定 SnapshotGenerationAgentPublisherSecurity 的 SqlStandardLogin 和 SqlStandardPassword (或 SecureSqlStandardPassword) 欄位。
(選擇性) 使用包含的邏輯 OR 運算子 (Visual C# 中的 | 和 Visual Basic 中的 Or),並使用排除的邏輯 OR 運算子 (Visual C# 中的 ^ 和 Visual Basic 中的 Xor),為 Attributes 屬性設定 PublicationAttributes 值。
(選擇性) 當散發者為非 SQL Server 散發者時,將 PublisherName 設定為散發者的名稱。
呼叫 Create 方法來建立發行集。
安全性注意事項 當利用遠端散發者來設定發行者時,提供給所有屬性的值 (包括 SnapshotGenerationAgentProcessSecurity) 都會以純文字的方式傳給散發者。您應該先加密發行者及其遠端散發者之間的連接,再呼叫 Create 方法。如需詳細資訊,請參閱<加密 SQL Server 的連接>。
呼叫 CreateSnapshotAgent 方法來為發行集建立快照集代理程式作業。
建立合併式發行集
使用 ServerConnection 類別建立與發行者的連接。
為發行集資料庫建立 ReplicationDatabase 類別的執行個體、將 ConnectionContext 屬性設定為步驟 1 中的 ServerConnection 執行個體,並呼叫 LoadProperties 方法。如果 LoadProperties 傳回 false,請確認此資料庫確實存在。
如果 EnabledMergePublishing 屬性為 false,請將它設定為 true 然後呼叫 CommitPropertyChanges。
建立 MergePublication 類別的執行個體,並為此物件設定下列屬性:
將 ConnectionContext 設定為步驟 1 中的 ServerConnection。
將 DatabaseName 設為發行的資料庫名稱。
將 Name 設定為發行集名稱。
SnapshotGenerationAgentProcessSecurity 的 Login 和 Password 欄位,可提供執行快照集代理程式所使用之 Windows 帳戶的認證。當快照集代理程式要連接本機散發者及進行任何遠端連接 (使用 Windows 驗證) 時,也會使用此帳戶。
[!附註]
當發行集是由 sysadmin 固定伺服器角色的成員所建立時,不需要設定 SnapshotGenerationAgentProcessSecurity。如需詳細資訊,請參閱<複寫代理程式安全性模型>。
(選擇性) 使用包含的邏輯 OR 運算子 (Visual C# 中的 | 和 Visual Basic 中的 Or),並使用排除的邏輯 OR 運算子 (Visual C# 中的 ^ 和 Visual Basic 中的 Xor),為 Attributes 屬性設定 PublicationAttributes 值。
呼叫 Create 方法來建立發行集。
安全性注意事項 當利用遠端散發者來設定發行者時,提供給所有屬性的值 (包括 SnapshotGenerationAgentProcessSecurity) 都會以純文字的方式傳給散發者。您應該先加密發行者及其遠端散發者之間的連接,再呼叫 Create 方法。如需詳細資訊,請參閱<加密 SQL Server 的連接>。
呼叫 CreateSnapshotAgent 方法來為發行集建立快照集代理程式作業。
範例
此範例會針對交易式發行啟用 AdventureWorks 資料庫、定義記錄讀取器代理程式作業,並建立 AdvWorksProductTran 發行集。必須為此發行集定義發行項。建立記錄讀取器代理程式作業和快照集代理程式作業所需的 Windows 帳戶認證是在執行階段所傳遞。若要了解如何使用 RMO 來定義快照發行項和交易式發行項,請參閱<如何:定義發行項 (RMO 程式設計)>。
// Set the Publisher, publication database, and publication names.
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks";
string publisherName = publisherInstance;
ReplicationDatabase publicationDb;
TransPublication publication;
// Create a connection to the Publisher using Windows Authentication.
ServerConnection conn;
conn = new ServerConnection(publisherName);
try
{
// Connect to the Publisher.
conn.Connect();
// Enable the AdventureWorks database for transactional publishing.
publicationDb = new ReplicationDatabase(publicationDbName, conn);
// If the database exists and is not already enabled,
// enable it for transactional publishing.
if (publicationDb.LoadProperties())
{
if (!publicationDb.EnabledTransPublishing)
{
publicationDb.EnabledTransPublishing = true;
}
// If the Log Reader Agent does not exist, create it.
if (!publicationDb.LogReaderAgentExists)
{
// Specify the Windows account under which the agent job runs.
// This account will be used for the local connection to the
// Distributor and all agent connections that use Windows Authentication.
publicationDb.LogReaderAgentProcessSecurity.Login = winLogin;
publicationDb.LogReaderAgentProcessSecurity.Password = winPassword;
// Explicitly set authentication mode for the Publisher connection
// to the default value of Windows Authentication.
publicationDb.LogReaderAgentPublisherSecurity.WindowsAuthentication = true;
// Create the Log Reader Agent job.
publicationDb.CreateLogReaderAgent();
}
}
else
{
throw new ApplicationException(String.Format(
"The {0} database does not exist at {1}.",
publicationDb, publisherName));
}
// Set the required properties for the transactional publication.
publication = new TransPublication();
publication.ConnectionContext = conn;
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
// Specify a transactional publication (the default).
publication.Type = PublicationType.Transactional;
// Activate the publication so that we can add subscriptions.
publication.Status = State.Active;
// Enable push and pull subscriptions and independent Distribition Agents.
publication.Attributes |= PublicationAttributes.AllowPull;
publication.Attributes |= PublicationAttributes.AllowPush;
publication.Attributes |= PublicationAttributes.IndependentAgent;
// Specify the Windows account under which the Snapshot Agent job runs.
// This account will be used for the local connection to the
// Distributor and all agent connections that use Windows Authentication.
publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin;
publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword;
// Explicitly set the security mode for the Publisher connection
// Windows Authentication (the default).
publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = true;
if (!publication.IsExistingObject)
{
// Create the transactional publication.
publication.Create();
// Create a Snapshot Agent job for the publication.
publication.CreateSnapshotAgent();
}
else
{
throw new ApplicationException(String.Format(
"The {0} publication already exists.", publicationName));
}
}
catch (Exception ex)
{
// Implement custom application error handling here.
throw new ApplicationException(String.Format(
"The publication {0} could not be created.", publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Set the Publisher, publication database, and publication names.
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks"
Dim publisherName As String = publisherInstance
Dim publicationDb As ReplicationDatabase
Dim publication As TransPublication
' Create a connection to the Publisher using Windows Authentication.
Dim conn As ServerConnection
conn = New ServerConnection(publisherName)
Try
' Connect to the Publisher.
conn.Connect()
' Enable the AdventureWorks database for transactional publishing.
publicationDb = New ReplicationDatabase(publicationDbName, conn)
' If the database exists and is not already enabled,
' enable it for transactional publishing.
If publicationDb.LoadProperties() Then
If Not publicationDb.EnabledTransPublishing Then
publicationDb.EnabledTransPublishing = True
End If
' If the Log Reader Agent does not exist, create it.
If Not publicationDb.LogReaderAgentExists Then
' Specify the Windows account under which the agent job runs.
' This account will be used for the local connection to the
' Distributor and all agent connections that use Windows Authentication.
publicationDb.LogReaderAgentProcessSecurity.Login = winLogin
publicationDb.LogReaderAgentProcessSecurity.Password = winPassword
' Explicitly set authentication mode for the Publisher connection
' to the default value of Windows Authentication.
publicationDb.LogReaderAgentPublisherSecurity.WindowsAuthentication = True
' Create the Log Reader Agent job.
publicationDb.CreateLogReaderAgent()
End If
Else
Throw New ApplicationException(String.Format( _
"The {0} database does not exist at {1}.", _
publicationDb, publisherName))
End If
' Set the required properties for the transactional publication.
publication = New TransPublication()
publication.ConnectionContext = conn
publication.Name = publicationName
publication.DatabaseName = publicationDbName
' Specify a transactional publication (the default).
publication.Type = PublicationType.Transactional
'Enable push and pull subscriptions and independent Distribition Agents.
publication.Attributes = _
publication.Attributes Or PublicationAttributes.AllowPull
publication.Attributes = _
publication.Attributes Or PublicationAttributes.AllowPush
publication.Attributes = _
publication.Attributes Or PublicationAttributes.IndependentAgent
' Activate the publication so that we can add subscriptions.
publication.Status = State.Active
' Specify the Windows account under which the Snapshot Agent job runs.
' This account will be used for the local connection to the
' Distributor and all agent connections that use Windows Authentication.
publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin
publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword
' Explicitly set the security mode for the Publisher connection
' Windows Authentication (the default).
publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = True
If Not publication.IsExistingObject Then
' Create the transactional publication.
publication.Create()
' Create a Snapshot Agent job for the publication.
publication.CreateSnapshotAgent()
Else
Throw New ApplicationException(String.Format( _
"The {0} publication already exists.", publicationName))
End If
Catch ex As Exception
' Implement custom application error handling here.
Throw New ApplicationException(String.Format( _
"The publication {0} could not be created.", publicationName), ex)
Finally
conn.Disconnect()
End Try
此範例會針對合併發行啟用 AdventureWorks 資料庫,並建立 AdvWorksSalesOrdersMerge 發行集。仍然必須為此發行集定義發行項。建立快照集代理程式作業所需的 Windows 帳戶認證是在執行階段所傳遞。若要了解如何使用 RMO 來定義合併發行項,請參閱<如何:定義發行項 (RMO 程式設計)>。
// Set the Publisher, publication database, and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks";
ReplicationDatabase publicationDb;
MergePublication publication;
// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);
try
{
// Connect to the Publisher.
conn.Connect();
// Enable the database for merge publication.
publicationDb = new ReplicationDatabase(publicationDbName, conn);
if (publicationDb.LoadProperties())
{
if (!publicationDb.EnabledMergePublishing)
{
publicationDb.EnabledMergePublishing = true;
}
}
else
{
// Do something here if the database does not exist.
throw new ApplicationException(String.Format(
"The {0} database does not exist on {1}.",
publicationDb, publisherName));
}
// Set the required properties for the merge publication.
publication = new MergePublication();
publication.ConnectionContext = conn;
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
// Enable precomputed partitions.
publication.PartitionGroupsOption = PartitionGroupsOption.True;
// Specify the Windows account under which the Snapshot Agent job runs.
// This account will be used for the local connection to the
// Distributor and all agent connections that use Windows Authentication.
publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin;
publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword;
// Explicitly set the security mode for the Publisher connection
// Windows Authentication (the default).
publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = true;
// Enable Subscribers to request snapshot generation and filtering.
publication.Attributes |= PublicationAttributes.AllowSubscriberInitiatedSnapshot;
publication.Attributes |= PublicationAttributes.DynamicFilters;
// Enable pull and push subscriptions.
publication.Attributes |= PublicationAttributes.AllowPull;
publication.Attributes |= PublicationAttributes.AllowPush;
if (!publication.IsExistingObject)
{
// Create the merge publication.
publication.Create();
// Create a Snapshot Agent job for the publication.
publication.CreateSnapshotAgent();
}
else
{
throw new ApplicationException(String.Format(
"The {0} publication already exists.", publicationName));
}
}
catch (Exception ex)
{
// Implement custom application error handling here.
throw new ApplicationException(String.Format(
"The publication {0} could not be created.", publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Set the Publisher, publication database, and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks"
Dim publicationDb As ReplicationDatabase
Dim publication As MergePublication
' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)
Try
' Connect to the Publisher.
conn.Connect()
' Enable the database for merge publication.
publicationDb = New ReplicationDatabase(publicationDbName, conn)
If publicationDb.LoadProperties() Then
If Not publicationDb.EnabledMergePublishing Then
publicationDb.EnabledMergePublishing = True
End If
Else
' Do something here if the database does not exist.
Throw New ApplicationException(String.Format( _
"The {0} database does not exist on {1}.", _
publicationDb, publisherName))
End If
' Set the required properties for the merge publication.
publication = New MergePublication()
publication.ConnectionContext = conn
publication.Name = publicationName
publication.DatabaseName = publicationDbName
' Enable precomputed partitions.
publication.PartitionGroupsOption = PartitionGroupsOption.True
' Specify the Windows account under which the Snapshot Agent job runs.
' This account will be used for the local connection to the
' Distributor and all agent connections that use Windows Authentication.
publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin
publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword
' Explicitly set the security mode for the Publisher connection
' Windows Authentication (the default).
publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = True
' Enable Subscribers to request snapshot generation and filtering.
publication.Attributes = publication.Attributes Or _
PublicationAttributes.AllowSubscriberInitiatedSnapshot
publication.Attributes = publication.Attributes Or _
PublicationAttributes.DynamicFilters
' Enable pull and push subscriptions
publication.Attributes = publication.Attributes Or _
PublicationAttributes.AllowPull
publication.Attributes = publication.Attributes Or _
PublicationAttributes.AllowPush
If Not publication.IsExistingObject Then
' Create the merge publication.
publication.Create()
' Create a Snapshot Agent job for the publication.
publication.CreateSnapshotAgent()
Else
Throw New ApplicationException(String.Format( _
"The {0} publication already exists.", publicationName))
End If
Catch ex As Exception
' Implement custom application error handling here.
Throw New ApplicationException(String.Format( _
"The publication {0} could not be created.", publicationName), ex)
Finally
conn.Disconnect()
End Try