サブスクリプションを同期する方法 (プログラム)

ここでは、SqlCeReplication クラスを使用して Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) でサブスクリプションを同期する方法について学習します。SqlServerCe 名前空間の使用については、SqlServerCe 名前空間のリファレンス ドキュメントを参照してください。

手順

  1. SqlCeReplication オブジェクトを初期化します。

    SqlCeReplication repl = new SqlCeReplication();
    
  2. 接続プロパティを設定します。これらのプロパティでは、サブスクライブするパブリケーションの名前と場所、ローカルの SQL Server Compact Edition データベースの名前と場所、および SQL Server Compact Edition サーバー エージェントの場所を指定します。

    repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.dll";
    repl.InternetLogin = "MyInternetLogin";
    repl.InternetPassword = "<password>";
    repl.Publisher = "MyPublisher";
    repl.PublisherDatabase = "MyPublisherDatabase";
    repl.PublisherLogin = "MyPublisherLogin";
    repl.PublisherPassword = "<password>";
    repl.Publication = "MyPublication";
    repl.Subscriber = "MySubscriber";
    repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";
    
  3. AddSubscription メソッドを呼び出し、サブスクリプションを作成します。

    repl.AddSubscription(AddOption.CreateDatabase);
    
  4. Synchronize メソッドを呼び出し、データ同期を実行して、パブリケーションのデータを含むローカル データベースを作成します。

    repl.Synchronize();
    

使用例

次の例では、Microsoft SQL Server を使用してサブスクリプションを作成し、データを同期します。この例では、ローカル データベースの名前は MyDatabase.sdf、パブリケーションの名前は MyPublication としています。

SqlCeReplication repl = null;

        try
        {
            // Create an instance of and configure the SqlCeReplication object
            //
            repl = new SqlCeReplication();
            repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.dll";
            repl.InternetLogin = "MyInternetLogin";
            repl.InternetPassword = "<password>";
            repl.Publisher = "MyPublisher";
            repl.PublisherDatabase = "MyPublisherDatabase";
            repl.PublisherLogin = "MyPublisherLogin";
            repl.PublisherPassword = "<password>";
            repl.Publication = "MyPublication";
            repl.Subscriber = "MySubscriber";
            repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";

            // Create a local SQL Server Compact Edition Database subscription
            //
            repl.AddSubscription(AddOption.CreateDatabase);

            // Synchronize to the SQL Server database
            //
            repl.Synchronize();
        }
        catch (SqlCeException)
        {
            // Handle errors here
            //
        }
        finally
        {
            // Dispose the repl object
            //
            repl.Dispose();
        }
Dim repl As SqlCeReplication = Nothing

        Try
            ' Create an instance of and configure the SqlCeReplication object
            '
            repl = New SqlCeReplication()
            repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa30.dll"
            repl.InternetLogin = "MyInternetLogin"
            repl.InternetPassword = "<password>"
            repl.Publisher = "MyPublisher"
            repl.PublisherDatabase = "MyPublisherDatabase"
            repl.PublisherLogin = "MyPublisherLogin"
            repl.PublisherPassword = "<password>"
            repl.Publication = "MyPublication"
            repl.Subscriber = "MySubscriber"
            repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf"

            ' Create the local SQL Server Compact Edition Database subscription
            '
            repl.AddSubscription(AddOption.CreateDatabase)

            ' Synchronize to SQL Server to populate the Subscription 
            '
            repl.Synchronize()
        Catch
            ' Handle errors here
            '
        Finally
            ' Dispose the repl object
            '
            repl.Dispose()
        End Try
ISSCEMerge      *pISSCEMerge = NULL;
   ISSCEErrors  *pISSCEErrors = NULL;
   HRESULT          hr;
   BSTR            bstr = NULL;
   BOOL            fInitialized = FALSE;
   LONG            lPubChanges;
   LONG            lPubConflicts;
   LONG            lSubChanges;

   /* Create the Replication object. */
   CoCreateInstance(CLSID_Replication, NULL, CLSCTX_INPROC_SERVER,
      IID_ISSCEMerge, (LPVOID *) &pISSCEMerge);
   
  /* Set Internet properties */
   bstr = SysAllocString
     (L"https://www.adventure-works.com/sqlce/sqlcesa30.dll");
   pISSCEMerge->put_InternetURL(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"MyInternetLogin");
   pISSCEMerge->put_InternetLogin(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"<MyInternetPassword>");
   pISSCEMerge->put_InternetPassword(bstr);
   SysFreeString(bstr);
   
   /* Set Publisher properties. */
   bstr = SysAllocString(L"SamplePublisher");
   pISSCEMerge->put_Publisher(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"AdventureWorks_SQLCE");
   pISSCEMerge->put_PublisherDatabase(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"SQLCEReplDemo");
   pISSCEMerge->put_Publication(bstr);
   SysFreeString(bstr);

   pISSCEMerge->put_PublisherSecurityMode(NT_AUTHENTICATION);

   /* Set Subscriber properties. */
   bstr = SysAllocString(L"Data Source=\\Ssce.sdf");
   pISSCEMerge->put_SubscriberConnectionString(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"SQLCE Sub #1");
   pISSCEMerge->put_Subscriber(bstr);
   SysFreeString(bstr);

   /* Perform the synchronization. */
   hr = pISSCEMerge->Initialize();
   if (SUCCEEDED(hr))
      {
      fInitialized = TRUE;
      hr = pISSCEMerge->Run();
      }

   if (SUCCEEDED(hr))
      {
      pISSCEMerge->get_PublisherChanges(&lPubChanges);
      pISSCEMerge->get_PublisherConflicts(&lPubConflicts);
      pISSCEMerge->get_SubscriberChanges(&lSubChanges);
      }
   else
      {
   if(SUCCEEDED(hr = pISSCEMerge->get_ErrorRecords(&pISSCEErrors)))
      {
      ShowErrors(pISSCEErrors);
      pISSCEErrors->Release();
      };
      }

   if (fInitialized)
      {
      (void)pISSCEMerge->Terminate();
      }

参照

概念

マージ レプリケーションの使用
パブリケーションのサブスクライブ (SQL Server Compact Edition)
サブスクリプションの作成

ヘルプおよび情報

SQL Server Compact Edition のサポートについて