同步處理提取訂閱
適用於:SQL Server Azure SQL 受控執行個體
本主題說明如何使用 SQL Server Management Studio、複寫代理程式或 Replication Management Objects (RMO),在 SQL Server 中同步發送訂閱。
本主題內容
若要同步處理提取訂閱,請使用:
使用 SQL Server Management Studio
訂閱是由散發代理程式 (適用於快照式與異動複寫) 或合併代理程式 (適用於合併式複寫) 同步處理。 代理程式可以繼續執行、視需要執行,或是依照排程執行。 如需指定同步處理排程的詳細資訊,請參閱指定同步處理排程。
從 SQL Server Management Studio 中的 [本機訂閱] 資料夾視需要同步處理訂閱。
若要在 Management Studio 中視需要同步處理提取訂閱
連線到 Management Studio 中的訂閱者,然後展開伺服器節點。
展開 [複寫] 資料夾,然後展開 [本機訂閱] 資料夾。
以滑鼠右鍵按一下您要同步處理的訂閱,然後按一下 [檢視同步處理的狀態] 。
在 [檢視同步處理的狀態 - <訂閱者>:<訂閱資料庫>] 對話方塊中,按一下 [開始]。 同步處理完成後,會顯示 [同步處理已完成] 的訊息。
按一下 [關閉] 。
複寫代理程式
提取訂閱可透過程式設計方式加以同步處理,以及視需要從命令提示字元叫用適當的複寫代理程式可執行檔加以同步處理。 叫用的複寫代理程式可執行檔取決於提取訂閱所屬的發行集類型。 如需詳細資訊,請參閱 Replication Agents。
注意
複寫代理程式會使用從命令提示字元啟動此代理程式之使用者的 Windows 驗證認證,連接到本機伺服器。 當使用「Windows 整合式驗證」連接到遠端伺服器時,也會使用這些 Windows 認證。
從命令提示字元或批次檔執行散發代理程式
從命令提示字元或批次檔中,執行 distrib.exe 來啟動 複寫散發代理程式,並指定下列命令列引數:
-Publisher
-PublisherDB
-Distributor
-DistributorSecurityMode = 1
-Subscriber
-SubscriberDB
-SubscriberSecurityMode = 1
-SubscriptionType = 1
無果您正在使用「SQL Server 驗證」,您也必須指定下列引數:
-DistributorLogin
-DistributorPassword
-DistributorSecurityMode = \@publisher_security_mode
-PublisherLogin
-PublisherPassword
-PublisherSecurityMode = \@publisher_security_mode
-SubscriberLogin
-SubscriberPassword
-SubscriberSecurityMode = 0
從命令提示字元或批次檔執行合併代理程式
從命令提示字元或批次檔中,執行 replmerg.exe 來啟動 複寫合併代理程式,並指定下列命令列引數:
-Publisher
-PublisherDB
-PublisherSecurityMode = 1
-Publication
-Distributor
-DistributorSecurityMode = 1
-Subscriber
-SubscriberSecurityMode = 1
-SubscriberDB
-SubscriptionType = 1
無果您正在使用「SQL Server 驗證」,您也必須指定下列引數:
-DistributorLogin
-DistributorPassword
-DistributorSecurityMode = \@publisher_security_mode
-PublisherLogin
-PublisherPassword
-PublisherSecurityMode = \@publisher_security_mode
-SubscriberLogin
-SubscriberPassword
-SubscriberSecurityMode = 0
範例 (複寫代理程式)
下列範例會啟動散發代理程式,以同步處理提取訂閱。 所有的連接都是使用「Windows 驗證」所建立。
-- Declare the variables.
SET Publisher=%instancename%
SET Subscriber=%instancename%
SET PublicationDB=AdventureWorks
SET SubscriptionDB=AdventureWorksReplica
SET Publication=AdvWorksProductsTran
-- Start the Distribution Agent.
-- The following command must be supplied without line breaks.
"C:\Program Files\Microsoft SQL Server\100\COM\DISTRIB.EXE" -Subscriber %Subscriber%
-SubscriberDB %SubscriptionDB% -SubscriberSecurityMode 1 -Publication %Publication%
-Publisher %Publisher% -PublisherDB %PublicationDB% -Distributor %Publisher%
-DistributorSecurityMode 1 -Continuous -SubscriptionType 1;
下列範例會啟動合併代理程式,以同步處理提取訂閱。 所有的連接都是使用「Windows 驗證」所建立。
-- Declare the variables.
SET Publisher=%instancename%
SET Subscriber=%instancename%
SET PublicationDB=AdventureWorks
SET SubscriptionDB=AdventureWorksReplica
SET Publication=AdvWorksSalesOrdersMerge
--Start the Merge Agent with concurrent upload and download processes.
-- The following command must be supplied without line breaks.
"C:\Program Files\Microsoft SQL Server\100\COM\REPLMERG.EXE" -Publication %Publication%
-Publisher %Publisher% -Subscriber %Subscriber% -Distributor %Publisher%
-PublisherDB %PublicationDB% -SubscriberDB %SubscriptionDB% -PublisherSecurityMode 1
-OutputVerboseLevel 2 -SubscriberSecurityMode 1 -SubscriptionType 1 -DistributorSecurityMode 1
-Validate 3 -ParallelUploadDownload 1 ;
使用 Replication Management Objects (RMO)
您可以使用 Replication Management Objects (RMO) 和對複寫代理程式功能的 Managed 程式碼存取,以程式設計的方式同步處理提取訂閱。 用於同步處理提取訂閱的類別依該訂閱所屬的發行集類型而定。
注意
如果您要啟動自發執行的同步處理而不影響應用程式,請非同步啟動代理程式。 不過,如果要監視同步處理的結果並在同步處理期間從代理程式接收回撥 (例如,顯示進度列),您就應該同步啟動代理程式。 針對 Microsoft SQL Server 2005 Express Edition 訂閱者,您必須同步啟動代理程式。
若要同步處理快照式或交易式發行集的提取訂閱
使用 ServerConnection 類別建立與「訂閱者」的連接。
建立 TransPullSubscription 類別的執行個體,並設定下列屬性:
將 DatabaseName設為訂閱資料庫名稱。
將 PublicationName設為訂閱所屬發行集的名稱。
將 PublicationDBName設定為發行集資料庫名稱。
將 PublisherName設為「發行者」的名稱。
將 ConnectionContext設為步驟 1 中建立的連接。
呼叫 LoadProperties 方法以取得剩餘的訂閱屬性。 如果此方法傳回 false,請確認該訂閱存在。
以下列其中一種方式啟動「訂閱者」上的「散發代理程式」:
從步驟 2 的 SynchronizeWithJob 執行個體上呼叫 TransPullSubscription 方法。 此方法會以非同步方式啟動散發代理程式,並且控制項會在代理程式作業執行時立即傳回至應用程式。 您無法針對 SQL Server 2005 Express Edition 訂閱者呼叫此方法,或者如果訂閱是以適用於 CreateSyncAgentByDefault 的 false 值 (預設值) 建立的,也無法呼叫。
從 TransSynchronizationAgent 屬性取得 SynchronizationAgent 類別的執行個體,並呼叫 Synchronize 方法。 此方法會同步啟動代理程式,而控制項仍會停留於正在執行的代理程式作業。 在同步執行期間,您可以在代理程式執行時處理 Status 事件。
注意
如果您在建立提取訂閱時為 false for CreateSyncAgentByDefault 值 (預設值),則還需要指定 Distributor複寫代理程式 DistributorSecurityMode,以及 (選擇性) DistributorLogin 和 DistributorPassword ,這是因為在 MSsubscription_properties>。
若要同步處理合併式發行集的提取訂閱
使用 ServerConnection 類別建立與「訂閱者」的連接。
建立 MergePullSubscription 類別的執行個體,並設定下列屬性:
將 DatabaseName設為訂閱資料庫名稱。
將 PublicationName設為訂閱所屬發行集的名稱。
將 PublicationDBName設為發行的資料庫名稱。
將 PublisherName設為「發行者」的名稱。
將 ConnectionContext設為步驟 1 中建立的連接。
呼叫 LoadProperties 方法以取得剩餘的訂閱屬性。 如果此方法傳回 false,請確認該訂閱存在。
以下列其中一種方式啟動「訂閱者」上的「合併代理程式」:
從步驟 2 的 SynchronizeWithJob 執行個體上呼叫 MergePullSubscription 方法。 此方法會以非同步方式啟動合併代理程式,並且控制項會在代理程式作業執行時立即傳回至應用程式。 您無法針對 SQL Server 2005 Express Edition 訂閱者呼叫此方法,或者如果訂閱是以適用於 CreateSyncAgentByDefault 的 false 值 (預設值) 建立的,也無法呼叫。
從 MergeSynchronizationAgent 屬性取得 SynchronizationAgent 類別的執行個體,並呼叫 Synchronize 方法。 此方法會同步啟動「合併代理程式」,而控制項仍會停留於正在執行的代理程式作業。 在同步執行期間,您可以在代理程式執行時處理 Status 事件。
注意
如果您在建立提取訂閱時為 false for CreateSyncAgentByDefault 值 (預設值),則還需要指定 Distributor複寫代理程式 DistributorSecurityMode複寫代理程式 PublisherSecurityMode複寫代理程式 HostName複寫代理程式 SubscriptionType複寫代理程式 ExchangeType,以及 (選擇性) DistributorLogin複寫代理程式 DistributorPassword複寫代理程式 PublisherLogin和 PublisherPassword ,這是因為在 MSsubscription_properties>。
範例 (RMO)
此範例同步處理交易式發行集的提取訂閱,其中代理程式會使用代理程式作業非同步啟動。
// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksProductTran";
String publicationDbName = "AdventureWorks";
String subscriptionDbName = "AdventureWorksReplica";
// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);
TransPullSubscription subscription;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define subscription properties.
subscription = new TransPullSubscription();
subscription.ConnectionContext = conn;
subscription.DatabaseName = subscriptionDbName;
subscription.PublisherName = publisherName;
subscription.PublicationDBName = publicationDbName;
subscription.PublicationName = publicationName;
// If the pull subscription and the job exists, start the agent job.
if (subscription.LoadProperties() && subscription.AgentJobId != null)
{
subscription.SynchronizeWithJob();
}
else
{
// Do something here if the subscription does not exist.
throw new ApplicationException(String.Format(
"A subscription to '{0}' does not exists on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Do appropriate error handling here.
throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
conn.Disconnect();
}
' Define server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks"
Dim subscriptionDbName As String = "AdventureWorksReplica"
' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
Dim subscription As TransPullSubscription
Try
' Connect to the Subscriber.
conn.Connect()
' Define subscription properties.
subscription = New TransPullSubscription()
subscription.ConnectionContext = conn
subscription.DatabaseName = subscriptionDbName
subscription.PublisherName = publisherName
subscription.PublicationDBName = publicationDbName
subscription.PublicationName = publicationName
' If the pull subscription and the job exists, start the agent job.
If subscription.LoadProperties() And Not subscription.AgentJobId Is Nothing Then
subscription.SynchronizeWithJob()
Else
' Do something here if the subscription does not exist.
Throw New ApplicationException(String.Format( _
"A subscription to '{0}' does not exists on {1}", _
publicationName, subscriberName))
End If
Catch ex As Exception
' Do appropriate error handling here.
Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
conn.Disconnect()
End Try
此範例同步處理交易式發行集的提取訂閱,其中代理程式會同步啟動。
// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string subscriptionDbName = "AdventureWorksReplica";
string publicationDbName = "AdventureWorks";
// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);
TransPullSubscription subscription;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define the pull subscription.
subscription = new TransPullSubscription();
subscription.ConnectionContext = conn;
subscription.DatabaseName = subscriptionDbName;
subscription.PublisherName = publisherName;
subscription.PublicationDBName = publicationDbName;
subscription.PublicationName = publicationName;
// If the pull subscription exists, then start the synchronization.
if (subscription.LoadProperties())
{
// Check that we have enough metadata to start the agent.
if (subscription.PublisherSecurity != null)
{
// Synchronously start the Distribution Agent for the subscription.
subscription.SynchronizationAgent.Synchronize();
}
else
{
throw new ApplicationException("There is insufficient metadata to " +
"synchronize the subscription. Recreate the subscription with " +
"the agent job or supply the required agent properties at run time.");
}
}
else
{
// Do something here if the pull subscription does not exist.
throw new ApplicationException(String.Format(
"A subscription to '{0}' does not exist on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Implement appropriate error handling here.
throw new ApplicationException("The subscription could not be " +
"synchronized. Verify that the subscription has " +
"been defined correctly.", ex);
}
finally
{
conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim subscriptionDbName As String = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"
' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
Dim subscription As TransPullSubscription
Try
' Connect to the Subscriber.
conn.Connect()
' Define the pull subscription.
subscription = New TransPullSubscription()
subscription.ConnectionContext = conn
subscription.DatabaseName = subscriptionDbName
subscription.PublisherName = publisherName
subscription.PublicationDBName = publicationDbName
subscription.PublicationName = publicationName
' If the pull subscription exists, then start the synchronization.
If subscription.LoadProperties() Then
' Check that we have enough metadata to start the agent.
If Not subscription.PublisherSecurity Is Nothing Then
' Write agent output to a log file.
subscription.SynchronizationAgent.Output = "distagent.log"
subscription.SynchronizationAgent.OutputVerboseLevel = 2
' Synchronously start the Distribution Agent for the subscription.
subscription.SynchronizationAgent.Synchronize()
Else
Throw New ApplicationException("There is insufficient metadata to " + _
"synchronize the subscription. Recreate the subscription with " + _
"the agent job or supply the required agent properties at run time.")
End If
Else
' Do something here if the pull subscription does not exist.
Throw New ApplicationException(String.Format( _
"A subscription to '{0}' does not exist on {1}", _
publicationName, subscriberName))
End If
Catch ex As Exception
' Implement appropriate error handling here.
Throw New ApplicationException("The subscription could not be " + _
"synchronized. Verify that the subscription has " + _
"been defined correctly.", ex)
Finally
conn.Disconnect()
End Try
此範例同步處理合併式發行集的提取訂閱,其中代理程式會使用代理程式作業非同步啟動。
// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksSalesOrdersMerge";
String publicationDbName = "AdventureWorks";
String subscriptionDbName = "AdventureWorksReplica";
// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);
MergePullSubscription subscription;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define subscription properties.
subscription = new MergePullSubscription();
subscription.ConnectionContext = conn;
subscription.DatabaseName = subscriptionDbName;
subscription.PublisherName = publisherName;
subscription.PublicationDBName = publicationDbName;
subscription.PublicationName = publicationName;
// If the pull subscription and the job exists, start the agent job.
if (subscription.LoadProperties() && subscription.AgentJobId != null)
{
subscription.SynchronizeWithJob();
}
else
{
// Do something here if the subscription does not exist.
throw new ApplicationException(String.Format(
"A subscription to '{0}' does not exists on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Do appropriate error handling here.
throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
conn.Disconnect();
}
' Define server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks"
Dim subscriptionDbName As String = "AdventureWorksReplica"
' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
Dim subscription As MergePullSubscription
Try
' Connect to the Subscriber.
conn.Connect()
' Define subscription properties.
subscription = New MergePullSubscription()
subscription.ConnectionContext = conn
subscription.DatabaseName = subscriptionDbName
subscription.PublisherName = publisherName
subscription.PublicationDBName = publicationDbName
subscription.PublicationName = publicationName
' If the pull subscription and the job exists, start the agent job.
If subscription.LoadProperties() And Not subscription.AgentJobId Is Nothing Then
subscription.SynchronizeWithJob()
Else
' Do something here if the subscription does not exist.
Throw New ApplicationException(String.Format( _
"A subscription to '{0}' does not exists on {1}", _
publicationName, subscriberName))
End If
Catch ex As Exception
' Do appropriate error handling here.
Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
conn.Disconnect()
End Try
此範例同步處理合併式發行集的提取訂閱,其中代理程式會同步啟動。
// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string subscriptionDbName = "AdventureWorksReplica";
string publicationDbName = "AdventureWorks";
// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);
MergePullSubscription subscription;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define the pull subscription.
subscription = new MergePullSubscription();
subscription.ConnectionContext = conn;
subscription.DatabaseName = subscriptionDbName;
subscription.PublisherName = publisherName;
subscription.PublicationDBName = publicationDbName;
subscription.PublicationName = publicationName;
// If the pull subscription exists, then start the synchronization.
if (subscription.LoadProperties())
{
// Check that we have enough metadata to start the agent.
if (subscription.PublisherSecurity != null || subscription.DistributorSecurity != null)
{
// Synchronously start the Merge Agent for the subscription.
subscription.SynchronizationAgent.Synchronize();
}
else
{
throw new ApplicationException("There is insufficient metadata to " +
"synchronize the subscription. Recreate the subscription with " +
"the agent job or supply the required agent properties at run time.");
}
}
else
{
// Do something here if the pull subscription does not exist.
throw new ApplicationException(String.Format(
"A subscription to '{0}' does not exist on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Implement appropriate error handling here.
throw new ApplicationException("The subscription could not be " +
"synchronized. Verify that the subscription has " +
"been defined correctly.", ex);
}
finally
{
conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim subscriptionDbName As String = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"
' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
Dim subscription As MergePullSubscription
Try
' Connect to the Subscriber.
conn.Connect()
' Define the pull subscription.
subscription = New MergePullSubscription()
subscription.ConnectionContext = conn
subscription.DatabaseName = subscriptionDbName
subscription.PublisherName = publisherName
subscription.PublicationDBName = publicationDbName
subscription.PublicationName = publicationName
' If the pull subscription exists, then start the synchronization.
If subscription.LoadProperties() Then
' Check that we have enough metadata to start the agent.
If Not subscription.PublisherSecurity Is Nothing Or subscription.DistributorSecurity Is Nothing Then
' Output agent messages to the console.
subscription.SynchronizationAgent.OutputVerboseLevel = 1
subscription.SynchronizationAgent.Output = ""
' Synchronously start the Merge Agent for the subscription.
subscription.SynchronizationAgent.Synchronize()
Else
Throw New ApplicationException("There is insufficient metadata to " + _
"synchronize the subscription. Recreate the subscription with " + _
"the agent job or supply the required agent properties at run time.")
End If
Else
' Do something here if the pull subscription does not exist.
Throw New ApplicationException(String.Format( _
"A subscription to '{0}' does not exist on {1}", _
publicationName, subscriberName))
End If
Catch ex As Exception
' Implement appropriate error handling here.
Throw New ApplicationException("The subscription could not be " + _
"synchronized. Verify that the subscription has " + _
"been defined correctly.", ex)
Finally
conn.Disconnect()
End Try
此範例使用 Web 同步處理來同步處理合併式發行集的提取訂閱。 該訂閱是在沒有代理程式作業和相關訂閱中繼資料的情況下建立的,因此必須同步啟動代理程式並提供其他訂閱資訊。
// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string distributorName = distributorInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string subscriptionDbName = "AdventureWorksReplica";
string publicationDbName = "AdventureWorks";
string hostname = @"adventure-works\garrett1";
string webSyncUrl = "https://" + publisherInstance + "/SalesOrders/replisapi.dll";
// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);
MergePullSubscription subscription;
MergeSynchronizationAgent agent;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define the pull subscription.
subscription = new MergePullSubscription();
subscription.ConnectionContext = conn;
subscription.DatabaseName = subscriptionDbName;
subscription.PublisherName = publisherName;
subscription.PublicationDBName = publicationDbName;
subscription.PublicationName = publicationName;
// If the pull subscription exists, then start the synchronization.
if (subscription.LoadProperties())
{
// Get the agent for the subscription.
agent = subscription.SynchronizationAgent;
// Check that we have enough metadata to start the agent.
if (agent.PublisherSecurityMode == null)
{
// Set the required properties that could not be returned
// from the MSsubscription_properties table.
agent.PublisherSecurityMode = SecurityMode.Integrated;
agent.DistributorSecurityMode = SecurityMode.Integrated;
agent.Distributor = publisherName;
agent.HostName = hostname;
// Set optional Web synchronization properties.
agent.UseWebSynchronization = true;
agent.InternetUrl = webSyncUrl;
agent.InternetSecurityMode = SecurityMode.Standard;
agent.InternetLogin = winLogin;
agent.InternetPassword = winPassword;
}
// Enable agent output to the console.
agent.OutputVerboseLevel = 1;
agent.Output = "";
// Synchronously start the Merge Agent for the subscription.
agent.Synchronize();
}
else
{
// Do something here if the pull subscription does not exist.
throw new ApplicationException(String.Format(
"A subscription to '{0}' does not exist on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Implement appropriate error handling here.
throw new ApplicationException("The subscription could not be " +
"synchronized. Verify that the subscription has " +
"been defined correctly.", ex);
}
finally
{
conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim subscriptionDbName As String = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"
Dim hostname As String = "adventure-works\garrett1"
Dim webSyncUrl As String = "https://" + publisherInstance + "/SalesOrders/replisapi.dll"
' Create a connection to the Subscriber.
Dim conn As ServerConnection = New ServerConnection(subscriberName)
Dim subscription As MergePullSubscription
Dim agent As MergeSynchronizationAgent
Try
' Connect to the Subscriber.
conn.Connect()
' Define the pull subscription.
subscription = New MergePullSubscription()
subscription.ConnectionContext = conn
subscription.DatabaseName = subscriptionDbName
subscription.PublisherName = publisherName
subscription.PublicationDBName = publicationDbName
subscription.PublicationName = publicationName
' If the pull subscription exists, then start the synchronization.
If subscription.LoadProperties() Then
' Get the agent for the subscription.
agent = subscription.SynchronizationAgent
' Check that we have enough metadata to start the agent.
If agent.PublisherSecurityMode = Nothing Then
' Set the required properties that could not be returned
' from the MSsubscription_properties table.
agent.PublisherSecurityMode = SecurityMode.Integrated
agent.Distributor = publisherInstance
agent.DistributorSecurityMode = SecurityMode.Integrated
agent.HostName = hostname
' Set optional Web synchronization properties.
agent.UseWebSynchronization = True
agent.InternetUrl = webSyncUrl
agent.InternetSecurityMode = SecurityMode.Standard
agent.InternetLogin = winLogin
agent.InternetPassword = winPassword
End If
' Enable agent logging to the console.
agent.OutputVerboseLevel = 1
agent.Output = ""
' Synchronously start the Merge Agent for the subscription.
agent.Synchronize()
Else
' Do something here if the pull subscription does not exist.
Throw New ApplicationException(String.Format( _
"A subscription to '{0}' does not exist on {1}", _
publicationName, subscriberName))
End If
Catch ex As Exception
' Implement appropriate error handling here.
Throw New ApplicationException("The subscription could not be " + _
"synchronized. Verify that the subscription has " + _
"been defined correctly.", ex)
Finally
conn.Disconnect()
End Try