如何:建立發送訂閱 (RMO 程式設計)
您可以使用「複寫管理物件」(RMO) 以程式設計的方式建立發送訂閱。用於建立發送訂閱的 RMO 類別依該訂閱所屬的發行集類型而定。
安全性注意事項: |
---|
可能的話,會在執行階段提示使用者輸入安全性認證。如果您必須儲存認證,請使用 Microsoft Windows .NET Framework 提供的密碼編譯服務。 |
若要建立快照式或交易式發行集的發送訂閱
使用 ServerConnection 類別建立到「發行者」的連接。
使用步驟 1 中的「發行者」連接建立 TransPublication 類別的執行個體。指定 Name、DatabaseName 和 ConnectionContext。
呼叫 LoadProperties 方法。如果此方法傳回 false,則表示步驟 2 中指定的屬性不正確,或伺服器上不存在該發行集。
在 Attributes 屬性和 AllowPush 之間執行位元運算邏輯 AND (Visual C# 中的 & 和 Visual Basic 中的 And)。如果結果為 None,則將 Attributes 設為 Attributes 和 AllowPush 之間位元運算邏輯 OR (Visual C# 中的 | 和 Visual Basic 中的 Or) 的結果。然後呼叫 CommitPropertyChanges 以啟用發送訂閱。
如果訂閱資料庫不存在,可使用 Database 類別建立它。如需詳細資訊,請參閱<Creating, Altering, and Removing Databases>。
建立 TransSubscription 類別的執行個體。
設定下列訂閱屬性:
- 將 ConnectionContext 設為到步驟 1 中建立之「發行者」的 ServerConnection。
- 將 SubscriptionDBName 設為訂閱資料庫的名稱。
- 將 SubscriberName 設為「訂閱者」的名稱。
- 將 DatabaseName 設為發行集資料庫的名稱。
- 將 PublicationName 設為發行集的名稱。
- SynchronizationAgentProcessSecurity 的 Login 和 Password (或 SecurePassword) 欄位,為「散發代理程式」在「散發者」上執行時所使用的 Microsoft Windows 帳戶提供認證。此帳戶用於建立到「散發者」的本機連接,以及使用「Windows 驗證」建立遠端連接。
附註: 當訂閱是由 sysadmin 固定伺服器角色的成員建立時,不需要設定 SynchronizationAgentProcessSecurity,但還是建議您對其進行設定。在這種情況下,代理程式會模擬「SQL Server 代理程式」帳戶。如需詳細資訊,請參閱<複寫代理程式安全性模型>。 - (選擇性) CreateSyncAgentByDefault 的 true 值 (預設值),以建立用於同步處理訂閱的代理程式作業。如果指定 false,則只能以程式設計的方式同步處理訂閱。
- (選擇性) 在使用「SQL Server 驗證」連接到「訂閱者」時,設定 SubscriberSecurity 的 SqlStandardLogin 和 SqlStandardPassword (或 SecureSqlStandardPassword) 欄位。
呼叫 Create 方法。
安全性注意事項: 當利用遠端「散發者」來設定「發行者」時,提供給所有屬性的值 (包括 SynchronizationAgentProcessSecurity) 都會以純文字的方式傳給「散發者」。您應該先加密「發行者」及其遠端「散發者」之間的連接,再呼叫 Create 方法。如需詳細資訊,請參閱<加密 SQL Server 的連接>。
若要建立合併式發行集的發送訂閱
使用 ServerConnection 類別建立到「發行者」的連接。
使用步驟 1 中的「發行者」連接建立 MergePublication 類別的執行個體。指定 Name、DatabaseName 和 ConnectionContext。
呼叫 LoadProperties 方法。如果此方法傳回 false,則表示步驟 2 中指定的屬性不正確,或伺服器上不存在該發行集。
在 Attributes 屬性和 AllowPush 之間執行位元運算邏輯 AND (Visual C# 中的 & 和 Visual Basic 中的 And)。如果結果為 None,則將 Attributes 設為 Attributes 和 AllowPush 之間位元運算邏輯 OR (Visual C# 中的 | 和 Visual Basic 中的 Or) 的結果。然後呼叫 CommitPropertyChanges 以啟用發送訂閱。
如果訂閱資料庫不存在,可使用 Database 類別建立它。如需詳細資訊,請參閱<Creating, Altering, and Removing Databases>。
建立 MergeSubscription 類別的執行個體。
設定下列訂閱屬性:
- 將 ConnectionContext 設為到步驟 1 中建立之「發行者」的 ServerConnection。
- 將 SubscriptionDBName 設為訂閱資料庫的名稱。
- 將 SubscriberName 設為「訂閱者」的名稱。
- 將 DatabaseName 設為發行集資料庫的名稱。
- 將 PublicationName 設為發行集的名稱。
- SynchronizationAgentProcessSecurity 的 Login 和 Password (或 SecurePassword) 欄位,為「合併代理程式」在「散發者」上執行時所使用的 Microsoft Windows 帳戶提供認證。此帳戶用於建立到「散發者」的本機連接,以及使用「Windows 驗證」建立遠端連接。
附註: 當訂閱是由 sysadmin 固定伺服器角色的成員建立時,不需要設定 SynchronizationAgentProcessSecurity,但還是建議您對其進行設定。在這種情況下,代理程式會模擬「SQL Server 代理程式」帳戶。如需詳細資訊,請參閱<複寫代理程式安全性模型>。 - (選擇性) CreateSyncAgentByDefault 的 true 值 (預設值),以建立用於同步處理訂閱的代理程式作業。如果指定 false,則只能以程式設計的方式同步處理訂閱。
- (選擇性) 在使用「SQL Server 驗證」連接到「訂閱者」時,設定 SubscriberSecurity 的 SqlStandardLogin 和 SqlStandardPassword (或 SecureSqlStandardPassword) 欄位。
- (選擇性) 在使用「SQL Server 驗證」連接到「發行者」時,設定 PublisherSecurity 的 SqlStandardLogin 和 SqlStandardPassword (或 SecureSqlStandardPassword) 欄位。
呼叫 Create 方法。
安全性注意事項: 當利用遠端「散發者」來設定「發行者」時,提供給所有屬性的值 (包括 SynchronizationAgentProcessSecurity) 都會以純文字的方式傳給「散發者」。您應該先加密「發行者」及其遠端「散發者」之間的連接,再呼叫 Create 方法。如需詳細資訊,請參閱<加密 SQL Server 的連接>。
範例
此範例會建立交易式發行集的新發送訂閱。用於執行「散發代理程式」作業的 Windows 帳戶認證是在執行階段傳遞的。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorksReplica";
string publicationDbName = "AdventureWorks";
//Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(subscriberName);
// Create the objects that we need.
TransPublication publication;
TransSubscription subscription;
try
{
// Connect to the Publisher.
conn.Connect();
// Ensure that the publication exists and that
// it supports push subscriptions.
publication = new TransPublication();
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
publication.ConnectionContext = conn;
if (publication.IsExistingObject)
{
if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
{
publication.Attributes |= PublicationAttributes.AllowPush;
}
// Define the push subscription.
subscription = new TransSubscription();
subscription.ConnectionContext = conn;
subscription.SubscriberName = subscriberName;
subscription.PublicationName = publicationName;
subscription.DatabaseName = publicationDbName;
subscription.SubscriptionDBName = subscriptionDbName;
// Specify the Windows login credentials for the Distribution Agent job.
subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
subscription.SynchronizationAgentProcessSecurity.Password = winPassword;
// By default, subscriptions to transactional publications are synchronized
// continuously, but in this case we only want to synchronize on demand.
subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.OnDemand;
// Create the push subscription.
subscription.Create();
}
else
{
// Do something here if the publication does not exist.
throw new ApplicationException(String.Format(
"The publication '{0}' does not exist on {1}.",
publicationName, publisherName));
}
}
catch (Exception ex)
{
// Implement the appropriate error handling here.
throw new ApplicationException(String.Format(
"The subscription to {0} could not be created.", publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksProductTran"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"
'Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
' Create the objects that we need.
Dim publication As TransPublication
Dim subscription As TransSubscription
Try
' Connect to the Publisher.
conn.Connect()
' Ensure that the publication exists and that
' it supports push subscriptions.
publication = New TransPublication()
publication.Name = publicationName
publication.DatabaseName = publicationDbName
publication.ConnectionContext = conn
If publication.IsExistingObject Then
If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
publication.Attributes = publication.Attributes _
Or PublicationAttributes.AllowPush
End If
' Define the push subscription.
subscription = New TransSubscription()
subscription.ConnectionContext = conn
subscription.SubscriberName = subscriberName
subscription.PublicationName = publicationName
subscription.DatabaseName = publicationDbName
subscription.SubscriptionDBName = subscriptionDbName
' Specify the Windows login credentials for the Distribution Agent job.
subscription.SynchronizationAgentProcessSecurity.Login = winLogin
subscription.SynchronizationAgentProcessSecurity.Password = winPassword
' By default, subscriptions to transactional publications are synchronized
' continuously, but in this case we only want to synchronize on demand.
subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.OnDemand
' Create the push subscription.
subscription.Create()
Else
' Do something here if the publication does not exist.
Throw New ApplicationException(String.Format( _
"The publication '{0}' does not exist on {1}.", _
publicationName, publisherName))
End If
Catch ex As Exception
' Implement the appropriate error handling here.
Throw New ApplicationException(String.Format( _
"The subscription to {0} could not be created.", publicationName), ex)
Finally
conn.Disconnect()
End Try
此範例會建立合併式發行集的新發送訂閱。用於執行「合併代理程式」作業的 Windows 帳戶認證是在執行階段傳遞的。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksSalesOrdersMerge";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorksReplica";
string publicationDbName = "AdventureWorks";
string hostname = @"adventure-works\garrett1";
//Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(subscriberName);
// Create the objects that we need.
MergePublication publication;
MergeSubscription subscription;
try
{
// Connect to the Publisher.
conn.Connect();
// Ensure that the publication exists and that
// it supports push subscriptions.
publication = new MergePublication();
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
publication.ConnectionContext = conn;
if (publication.IsExistingObject)
{
if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
{
publication.Attributes |= PublicationAttributes.AllowPush;
}
// Define the push subscription.
subscription = new MergeSubscription();
subscription.ConnectionContext = conn;
subscription.SubscriberName = subscriberName;
subscription.PublicationName = publicationName;
subscription.DatabaseName = publicationDbName;
subscription.SubscriptionDBName = subscriptionDbName;
subscription.HostName = hostname;
// Set a schedule to synchronize the subscription every 2 hours
// during weekdays from 6am to 10pm.
subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly;
subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32(0x003E);
subscription.AgentSchedule.FrequencyRecurrenceFactor = 1;
subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour;
subscription.AgentSchedule.FrequencySubDayInterval = 2;
subscription.AgentSchedule.ActiveStartDate = 20051108;
subscription.AgentSchedule.ActiveEndDate = 20071231;
subscription.AgentSchedule.ActiveStartTime = 060000;
subscription.AgentSchedule.ActiveEndTime = 100000;
// Specify the Windows login credentials for the Merge Agent job.
subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
subscription.SynchronizationAgentProcessSecurity.Password = winPassword;
// Create the push subscription.
subscription.Create();
}
else
{
// Do something here if the publication does not exist.
throw new ApplicationException(String.Format(
"The publication '{0}' does not exist on {1}.",
publicationName, publisherName));
}
}
catch (Exception ex)
{
// Implement the appropriate error handling here.
throw new ApplicationException(String.Format(
"The subscription to {0} could not be created.", publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"
Dim hostname As String = "adventure-works\garrett1"
'Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
' Create the objects that we need.
Dim publication As MergePublication
Dim subscription As MergeSubscription
Try
' Connect to the Publisher.
conn.Connect()
' Ensure that the publication exists and that
' it supports push subscriptions.
publication = New MergePublication()
publication.Name = publicationName
publication.DatabaseName = publicationDbName
publication.ConnectionContext = conn
If publication.IsExistingObject Then
If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
publication.Attributes = publication.Attributes _
Or PublicationAttributes.AllowPush
End If
' Define the push subscription.
subscription = New MergeSubscription()
subscription.ConnectionContext = conn
subscription.SubscriberName = subscriberName
subscription.PublicationName = publicationName
subscription.DatabaseName = publicationDbName
subscription.SubscriptionDBName = subscriptionDbName
subscription.HostName = hostname
' Set a schedule to synchronize the subscription every 2 hours
' during weekdays from 6am to 10pm.
subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Weekly
subscription.AgentSchedule.FrequencyInterval = Convert.ToInt32("0x003E", 16)
subscription.AgentSchedule.FrequencyRecurrenceFactor = 1
subscription.AgentSchedule.FrequencySubDay = ScheduleFrequencySubDay.Hour
subscription.AgentSchedule.FrequencySubDayInterval = 2
subscription.AgentSchedule.ActiveStartDate = 20051108
subscription.AgentSchedule.ActiveEndDate = 20071231
subscription.AgentSchedule.ActiveStartTime = 60000
subscription.AgentSchedule.ActiveEndTime = 100000
' Specify the Windows login credentials for the Merge Agent job.
subscription.SynchronizationAgentProcessSecurity.Login = winLogin
subscription.SynchronizationAgentProcessSecurity.Password = winPassword
' Create the push subscription.
subscription.Create()
Else
' Do something here if the publication does not exist.
Throw New ApplicationException(String.Format( _
"The publication '{0}' does not exist on {1}.", _
publicationName, publisherName))
End If
Catch ex As Exception
' Implement the appropriate error handling here.
Throw New ApplicationException(String.Format( _
"The subscription to {0} could not be created.", publicationName), ex)
Finally
conn.Disconnect()
End Try
請參閱
工作
如何:建立發送訂閱 (複寫 Transact-SQL 程式設計)
如何:同步處理發送訂閱 (RMO 程式設計)
其他資源
Programming with Replication Management Objects
How to: Create a Publication (RMO Programming)
訂閱發行集