このトピックでは、SQL Server Management Studio、Transact-SQL、またはレプリケーション管理オブジェクト (RMO) を使用して、SQL Server 2014 でプッシュ サブスクリプションを作成する方法について説明します。 SQL Server 以外のサブスクライバーのプッシュ サブスクリプションの作成については、「SQL Server 以外のサブスクライバー のサブスクリプションを作成する」を参照してください。
SQL Server Management Studio の使用
サブスクリプションの新規作成ウィザードを使用して、パブリッシャーまたはサブスクライバーでプッシュ サブスクリプションを作成します。 ウィザードのページに従って、次の操作を行います。
パブリッシャーとパブリケーションを指定します。
レプリケーション エージェントを実行する場所を選択します。 プッシュ サブスクリプションの場合は、パブリケーションの種類に応じて、[ディストリビューション エージェントの場所] ページまたは [マージ エージェントの場所] ページの [ディストリビューター (プッシュ サブスクリプション)] ですべてのエージェントを実行するを選択します。
サブスクライバーデータベースとサブスクリプションデータベースを指定します。
レプリケーション エージェントによって行われる接続に使用するログインとパスワードを指定します。
スナップショット パブリケーションとトランザクション パブリケーションのサブスクリプションの場合は、[ ディストリビューション エージェントのセキュリティ ] ページで資格情報を指定します。
マージ パブリケーションのサブスクリプションの場合は、[ マージ エージェントのセキュリティ ] ページで資格情報を指定します。
各エージェントに必要なアクセス許可については、 レプリケーション エージェントのセキュリティ モデルに関するセクションを参照してください。
同期スケジュールとサブスクライバーを初期化するタイミングを指定します。
マージ パブリケーションの追加オプションを指定する: サブスクリプションの種類; パラメーター化されたフィルター処理の値。
サブスクリプションの更新を許可するトランザクション パブリケーションの追加オプションを指定します。サブスクライバーがパブリッシャーで変更を直ちにコミットするか、キューに書き込むかを指定します。サブスクライバーからパブリッシャーへの接続に使用される資格情報。
必要に応じて、サブスクリプションをスクリプト化します。
パブリッシャーからプッシュ サブスクリプションを作成するには
Microsoft SQL Server Management Studio でパブリッシャーに接続し、サーバー ノードを展開します。
[レプリケーション] フォルダーを展開し、[ローカル パブリケーション] フォルダーを展開します。
1 つ以上のサブスクリプションを作成するパブリケーションを右クリックし、[ 新しいサブスクリプション] をクリックします。
サブスクリプションの新規作成ウィザードのページを完了します。
サブスクライバーからプッシュ サブスクリプションを作成するには
SQL Server Management Studio でサブスクライバーに接続し、サーバー ノードを展開します。
[レプリケーション] フォルダーを展開します。
[ローカル サブスクリプション] フォルダーを右クリックし、[新しいサブスクリプション] をクリックします。
サブスクリプションの新規作成ウィザードの [パブリケーション] ページで、[パブリッシャー] <から < または >Find Oracle Publisher を選択します。
[サーバーへの接続] ダイアログ ボックスでパブリッシャー に接続 します。
[パブリケーション] ページで パブリケーション を選択します。
サブスクリプションの新規作成ウィザードのページを完了します。
Transact-SQL の使用
プッシュ サブスクリプションは、レプリケーション ストアド プロシージャを使用してプログラムで作成できます。 使用されるストアド プロシージャは、サブスクリプションが属するパブリケーションの種類によって異なります。
重要
可能であれば、実行時にセキュリティ資格情報を入力するようにユーザーに求めます。 スクリプト ファイルに資格情報を格納する必要がある場合は、未承認のアクセスを防ぐためにファイルをセキュリティで保護する必要があります。
スナップショットまたはトランザクション パブリケーションへのプッシュ サブスクリプションを作成するには
パブリッシャー側のパブリケーション データベースで、sp_helppublicationを実行して、パブリケーションがプッシュ サブスクリプションをサポートしていることを確認します。
allow_pushの値が 1 の場合、プッシュ サブスクリプションがサポートされます。
allow_pushの値が 0 の場合は、sp_changepublicationを実行し、@propertyのallow_pushと@valueの
true
を指定します。
パブリッシャー側のパブリケーション データベースで、 sp_addsubscriptionを実行します。 @publication、@subscriber、@destination_dbを指定します。 @subscription_type の値としてpushを指定します。 サブスクリプションを更新する方法の詳細については、「トランザクション パブリケーションへの更新可能なサブスクリプションを作成する」を参照してください。
パブリッシャー側のパブリケーション データベースで、 sp_addpushsubscription_agentを実行します。 次を指定します。
@subscriber、@subscriber_db、および@publicationパラメーター。
ディストリビューターでのディストリビューション エージェントが@job_loginおよび@job_passwordの条件で実行される Microsoft Windows の資格情報。
注
Windows 統合認証を使用して行われる接続では、常に @job_login と @job_passwordで指定された Windows 資格情報が使用されます。 ディストリビューション エージェントは、常に Windows 統合認証を使用してディストリビューターにローカル接続します。 既定では、エージェントは Windows 統合認証を使用してサブスクライバーに接続します。
(省略可能)@subscriber_security_modeの値は 0、@subscriber_loginと@subscriber_passwordの場合は Microsoft SQL Server ログイン情報です。 サブスクライバーに接続するときに SQL Server 認証を使用する必要がある場合は、これらのパラメーターを指定します。
このサブスクリプションのディストリビューション エージェント ジョブのスケジュール。 詳細については、「 同期スケジュールの指定」を参照してください。
重要
リモート ディストリビューターを使用してパブリッシャーでプッシュ サブスクリプションを作成すると、 job_login や job_passwordを含むすべてのパラメーターに指定された値がプレーン テキストとしてディストリビューターに送信されます。 このストアド プロシージャを実行する前に、パブリッシャーとそのリモート ディストリビューター間の接続を暗号化する必要があります。 詳細については、「データベース エンジンへの暗号化接続の有効化 (SQL Server 構成マネージャー)」を参照してください。
マージ パブリケーションへのプッシュ サブスクリプションを作成するには
パブリッシャー側のパブリケーション データベースで、sp_helpmergepublicationを実行して、パブリケーションがプッシュ サブスクリプションをサポートしていることを確認します。
allow_pushの値が 1 の場合、パブリケーションはプッシュ サブスクリプションをサポートします。
allow_pushの値が 1 でない場合は、sp_changemergepublicationを実行し、@propertyのallow_pushと@valueの
true
を指定します。
パブリッシャー側のパブリケーション データベースで、次のパラメーターを指定 して、sp_addmergesubscriptionを実行します。
@publication。 これはパブリケーションの名前です。
@subscriber_type。 クライアント サブスクリプションの場合は、 ローカル を指定し、サーバー サブスクリプションの場合は グローバルを指定します。
@subscription_priority。 サーバー サブスクリプションの場合は、サブスクリプションの優先順位を指定します (0.00 から 99.99)。
詳細については、「 高度なマージ レプリケーションの競合の検出と解決」を参照してください。
パブリッシャー側のパブリケーション データベースで、 sp_addmergepushsubscription_agentを実行します。 次を指定します。
@subscriber、@subscriber_db、および@publicationパラメーター。
ディストリビューターのマージ エージェントが @job_login と @job_passwordに対して実行される Windows 資格情報。
注
Windows 統合認証を使用して行われる接続では、常に @job_login と @job_passwordで指定された Windows 資格情報が使用されます。 マージ エージェントは、常に Windows 統合認証を使用してディストリビューターへのローカル接続を行います。 既定では、エージェントは Windows 統合認証を使用してサブスクライバーに接続します。
(省略可能)@subscriber_security_modeの値は 0、@subscriber_loginと@subscriber_passwordの場合は SQL Server ログイン情報です。 サブスクライバーに接続するときに SQL Server 認証を使用する必要がある場合は、これらのパラメーターを指定します。
(省略可能) 0 の @publisher_security_mode 値と、@publisher_login と @publisher_password の SQL Server ログイン情報。 パブリッシャーに接続するときに SQL Server 認証を使用する必要がある場合は、これらの値を指定します。
このサブスクリプションのためのマージ エージェント ジョブのスケジュール。 詳細については、「 同期スケジュールの指定」を参照してください。
重要
リモート ディストリビューターを使用してパブリッシャーでプッシュ サブスクリプションを作成すると、 job_login や job_passwordを含むすべてのパラメーターに指定された値がプレーン テキストとしてディストリビューターに送信されます。 このストアド プロシージャを実行する前に、パブリッシャーとそのリモート ディストリビューター間の接続を暗号化する必要があります。 詳細については、「データベース エンジンへの暗号化接続の有効化 (SQL Server 構成マネージャー)」を参照してください。
例 (Transact-SQL)
次の例では、トランザクション パブリケーションへのプッシュ サブスクリプションを作成します。 ログインとパスワードの値は、 sqlcmd スクリプト変数を使用して実行時に指定されます。
-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables
-- on the command line and in SQL Server Management Studio, see the
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorks2012Replica';
--Add a push subscription to a transactional publication.
USE [AdventureWorks2012]
EXEC sp_addsubscription
@publication = @publication,
@subscriber = @subscriber,
@destination_db = @subscriptionDB,
@subscription_type = N'push';
--Add an agent job to synchronize the push subscription.
EXEC sp_addpushsubscription_agent
@publication = @publication,
@subscriber = @subscriber,
@subscriber_db = @subscriptionDB,
@job_login = $(Login),
@job_password = $(Password);
GO
次の例では、マージ パブリケーションへのプッシュ サブスクリプションを作成します。 ログインとパスワードの値は、 sqlcmd スクリプト変数を使用して実行時に指定されます。
-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables
-- on the command line and in SQL Server Management Studio, see the
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @hostname AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorksReplica';
SET @hostname = N'adventure-works\david8'
-- Add a push subscription to a merge publication.
USE [AdventureWorks2012];
EXEC sp_addmergesubscription
@publication = @publication,
@subscriber = @subscriber,
@subscriber_db = @subscriptionDB,
@subscription_type = N'push',
@hostname = @hostname;
--Add an agent job to synchronize the push subscription.
EXEC sp_addmergepushsubscription_agent
@publication = @publication,
@subscriber = @subscriber,
@subscriber_db = @subscriptionDB,
@job_login = $(Login),
@job_password = $(Password);
GO
レプリケーション管理オブジェクト (RMO) の使用
レプリケーション管理オブジェクト (RMO) を使用して、プログラムでプッシュ サブスクリプションを作成できます。 プッシュ サブスクリプションの作成に使用する RMO クラスは、サブスクリプションが作成されるパブリケーションの種類によって異なります。
重要
可能な場合は、実行時にセキュリティ資格情報の入力をユーザーに求めます。 資格情報を格納する必要がある場合は、Microsoft Windows .NET Framework によって提供される 暗号化サービス を使用します。
スナップショット パブリケーションまたはトランザクション パブリケーションへのプッシュ サブスクリプションを作成するには
ServerConnection クラスを使用してパブリッシャーへの接続を作成します。
手順 1. のパブリッシャー接続を使用して、 TransPublication クラスのインスタンスを作成します。 Name、DatabaseName、およびConnectionContextを指定します。
LoadProperties メソッドを呼び出します。 このメソッドが
false
を返す場合、手順 2 で指定したプロパティが正しくないか、サーバーにパブリケーションが存在しません。プロパティ
&
とAnd
の間でビット単位の論理 AND (Visual C# ではAttributes、Visual Basic ではAllowPush) を実行します。 結果がNoneの場合は、Attributesと|
の間のビットごとの論理 OR (Visual C# ではOr
、Visual Basic では Attributes) の結果にAllowPushを設定します。 次に、 CommitPropertyChanges を呼び出してプッシュ サブスクリプションを有効にします。サブスクリプション データベースが存在しない場合は、 Database クラスを使用して作成します。 詳細については、「 データベースの作成、変更、および削除」を参照してください。
TransSubscription クラスのインスタンスを作成します。
次のサブスクリプション プロパティを設定します。
ConnectionContextの手順 1 で作成したパブリッシャーへのServerConnection。
SubscriptionDBNameのサブスクリプション データベースの名前。
SubscriberNameのサブスクライバーの名前。
DatabaseNameのパブリケーション データベースの名前。
PublicationNameのパブリケーションの名前。
Microsoft Windows アカウントの資格情報を提供するために、ディストリビューターでディストリビューション エージェントが実行されるSynchronizationAgentProcessSecurityのLoginフィールドとPasswordフィールドまたはSecurePassword*フィールドを使用します。 このアカウントは、ディストリビューターへのローカル接続を行い、Windows 認証を使用してリモート接続を行うために使用されます。
注
SynchronizationAgentProcessSecurity固定サーバー ロールのメンバーによってサブスクリプションが作成される場合、
sysadmin
の設定は必要ありませんが、推奨されます。 この場合、エージェントは 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 で指定したプロパティが正しくないか、サーバーにパブリケーションが存在しません。プロパティ
&
とAnd
の間でビット単位の論理 AND (Visual C# ではAttributes、Visual Basic ではAllowPush) を実行します。 結果がNoneの場合は、Attributesと|
の間のビットごとの論理 OR (Visual C# ではOr
、Visual Basic では Attributes) の結果にAllowPushを設定します。 次に、 CommitPropertyChanges を呼び出してプッシュ サブスクリプションを有効にします。サブスクリプション データベースが存在しない場合は、 Database クラスを使用して作成します。 詳細については、「 データベースの作成、変更、および削除」を参照してください。
MergeSubscription クラスのインスタンスを作成します。
次のサブスクリプション プロパティを設定します。
ConnectionContextの手順 1 で作成したパブリッシャーへのServerConnection。
SubscriptionDBNameのサブスクリプション データベースの名前。
SubscriberNameのサブスクライバーの名前。
DatabaseNameのパブリケーション データベースの名前。
PublicationNameのパブリケーションの名前。
SynchronizationAgentProcessSecurityのLoginフィールド、Passwordフィールド、またはSecurePassword*フィールドは、ディストリビューターでマージ エージェントが実行される Microsoft Windows アカウントの資格情報を提供します。 このアカウントは、ディストリビューターへのローカル接続を行い、Windows 認証を使用してリモート接続を行うために使用されます。
注
SynchronizationAgentProcessSecurity固定サーバー ロールのメンバーによってサブスクリプションが作成される場合、
sysadmin
の設定は必要ありませんが、推奨されます。 この場合、エージェントは SQL Server エージェント アカウントを偽装します。 詳細については、「 レプリケーション エージェント セキュリティ モデル」を参照してください。サブスクリプションの同期に使用するエージェント ジョブを作成するために、CreateSyncAgentByDefaultには
true
(既定値) が設定されています。(省略可能)false
を指定した場合、サブスクリプションはプログラムでのみ同期できます。(省略可能)SQL Server 認証を使用してサブスクライバーに接続する場合は、SubscriberSecurityのSqlStandardLoginフィールドとSqlStandardPasswordフィールドまたはSecureSqlStandardPassword フィールドを設定します。
(省略可能)SQL Server 認証を使用してパブリッシャーに接続する場合は、SqlStandardLoginのSqlStandardPasswordフィールドとSecureSqlStandardPasswordフィールドまたはPublisherSecurity フィールドを設定します。
Create メソッドを呼び出します。
重要
リモート ディストリビューターを使用してパブリッシャーでプッシュ サブスクリプションを作成すると、 SynchronizationAgentProcessSecurityを含むすべてのプロパティに指定された値がプレーン テキストとしてディストリビューターに送信されます。 Create メソッドを呼び出す前に、パブリッシャーとそのリモート ディストリビューター間の接続を暗号化する必要があります。 詳細については、「データベース エンジンへの暗号化接続の有効化 (SQL Server 構成マネージャー)」を参照してください。
例 (RMO)
この例では、トランザクション パブリケーションへの新しいプッシュ サブスクリプションを作成します。 ディストリビューション エージェント ジョブの実行に使用する Windows アカウントの資格情報は、実行時に渡されます。
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksProductTran";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";
//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 = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"
'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 = "AdventureWorks2012Replica";
string publicationDbName = "AdventureWorks2012";
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 = "AdventureWorks2012Replica"
Dim publicationDbName As String = "AdventureWorks2012"
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
こちらもご覧ください
プッシュ サブスクリプションのプロパティの表示と変更
レプリケーション のセキュリティに関するベスト プラクティス
パブリケーションを作成する
レプリケーション管理オブジェクトの概念
プッシュ サブスクリプションの同期
パブリケーションを購読する
sqlcmd でのスクリプト変数の使用