共用方式為


如何:同步處理訂閱 (以程式設計的方式)

在此主題中,您將瞭解如何使用 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 協助