SQL Server Express を使ったレプリケーション その3

前回の続き

今回はRMO(Replication Management Objects)を使った同期プログラムを作成する。
作成はVisual Studio 2017 Communityを使ってC#で行う。

まずは、プロジェクトの新規作成で、空のプロジェクト(.Net Framework)テンプレートを選んで、プロジェクトを作成する。
ここで注意するのは、.Net Frameworkのバージョンである。SQL Serverが提供しているRMO等のアセンブリは、ランタイムバージョンが最新のものではない可能性がある。すべてのSQL Serverを調べたわけではないが、SQL Server 2012 Expressのアセンブリは、.Net Framework3.5であった。よってプロジェクトのフレームワークは、.Net Framework 3.5で作成する。
f:id:megusuritan:20180726232851j:plain

また、よくわからないけど、RMOのアセンブリだけ32bit版しか提供されていない。(多分、将来的に無くなるんだろうね)そこで、64bitアセンブリを混成させる場合、作成したプロジェクトのCPUアーキテクチャは、AnyCPUではなく、明示的に構成マネージャーで指定する必要がある。
f:id:megusuritan:20180726234012j:plain

プロジェクトを作成したら、下記リンクからソースコードを入手しよう。
code.msdn.microsoft.com

リンクを開いてコピペで作成する。Brandon Williamsさん、感謝です。
このソースコードWindows認証を使ってるので、前回まで構築してきた環境では、動きません。
Windows認証の部分をSQL認証に変更します。

// 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; 

上記のコードを下記に書き換え

/*
 If you specified a value of false for CreateSyncAgentByDefault
 (the default) when you created the pull subscription,
 you also need to specify Distributor, DistributorSecurityMode,
 PublisherSecurityMode, HostName, SubscriptionType, 
 ExchangeType, and optionally DistributorLogin, 
 DistributorPassword, PublisherLogin, and PublisherPassword
 because the agent job related metadata for the subscription 
 is not available in MSsubscription_properties. 
*/
agent.Distributor = publisherName;
agent.DistributorSecurityMode = SecurityMode.Standard;
agent.PublisherSecurityMode = SecurityMode.Standard;
agent.HostName = publisherName;
agent.SubscriptionType = SubscriptionOption.Pull;
agent.ExchangeType = MergeExchangeType.Bidirectional;
agent.DistributorLogin = "<SQL認証ログオン名>";
agent.DistributorPassword = "<パスワード>";
agent.PublisherLogin = "<SQL認証ログオン名>";
agent.PublisherPassword = "<パスワード>";

書き換えたコードのコメントは、次のリンクからもらいました。
How to: Synchronize a Pull Subscription (RMO Programming) | Microsoft Docs

なんだかですます調がおかしくなったな。まぁいいやorz
コンパイルを成功させるには、アセンブリ参照を追加しなければならない。
今回は、SQL Server2012 Expressをインストールしたので、次のパスから参照を追加する。

Microsoft.SqlServer.ConnectionInfo

C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll

Microsoft.SqlServer.Replication

C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Replication.dll

Microsoft.SqlServer.Rmo

C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Rmo.dll

<>

実行する前のチェック

サブスクラバーのPCで、エクスプローラを開き、左にあるツリーのネットワークをクリックする。
そこに、パブリッシャーサーバーが表示されない場合は、手動で表示する必要がある。
f:id:megusuritan:20180727010935j:plain

上図の操作をするとネットワーク資格情報の入力ダイアログが表示されるハズw
f:id:megusuritan:20180727011946j:plain
ユーザー名とパスワードは、パブリッシャーのスナップショットエージェントアカウントを入力して接続してみる。

下図のように、パブリッシャーの共有フォルダが表示され、中身が見れたらOK
f:id:megusuritan:20180727012544j:plain

コンパイルしたプログラムを実行してみよう

ひと手間かけたら、早速実行する。例外が発生せず、正常に終了すればオールOK

ああ疲れたわ今日はここまでにする。

次回はプログラムがエラーになった場合の対処を書くことにする。

寝るorz