共用方式為


HOW TO:啟始本機和遠端資料庫之間的同步處理

當您新增本機資料庫快取並在 [設定資料同步處理] 對話方塊中提供設定資訊後,還是必須將程式碼加入應用程式中,才能實際同步處理本機和遠端資料庫。 呼叫 SyncAgent 的 Synchronize 方法以啟始同步處理。

您必須了解,同步處理只會對遠端資料及本機資料庫 (.sdf) 之間的資料進行同步處理。 您應用程式中的資料還是必須從資料庫中重新載入。 記得使用本機資料庫的更新資料來重新載入您的應用程式資料來源。 例如,呼叫 TableAdapter.Fill 方法,將本機資料庫中更新過的資料載入資料集的資料表中。

[設定資料同步處理] 對話方塊包含 [顯示程式碼範例] 連結,提供的程式碼範例內含呼叫 Synchronize 方法的正確語法。 請將這個程式碼範例包含在必須執行同步處理的應用程式中。

注意事項注意事項

在下列指示的某些 Visual Studio 使用者介面項目中,您的電腦可能會顯示不同的名稱或位置:您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。如需詳細資訊,請參閱 Visual Studio 設定

程序

若要啟始本機和遠端資料庫之間的同步處理

  1. 在 [方案總管] 中按兩下 .sync 檔案,以開啟 [設定資料同步處理] 對話方塊。

  2. 在 [設定資料同步處理] 對話方塊中按一下 [顯示程式碼範例] 連結。

  3. 按一下 [複製程式碼到剪貼簿],然後按一下 [關閉]。

  4. 將您在上一個步驟中複製的程式碼範例貼入您的應用程式中, 也就是貼在您要啟始同步處理呼叫的位置。 下列程式碼顯示如何對已設定同步處理 Northwind 之 Customers 資料表的應用程式,啟始同步處理:

    ' Call the Synchronize method to synchronize
    ' data between local and remote databases.
    Dim syncAgent As NorthwindCacheSyncAgent = New NorthwindCacheSyncAgent()
    Dim syncStats As Microsoft.Synchronization.Data.SyncStatistics =
        syncAgent.Synchronize()
    
    ' After synchronizing the data, refill the
    ' table in the dataset.
    Me.CustomersTableAdapter.Fill(NorthwindDataSet.Customers)
    
    // Call the Synchronize method to synchronize
    // data between local and remote databases.
    NorthwindCacheSyncAgent syncAgent = new NorthwindCacheSyncAgent();
    Microsoft.Synchronization.Data.SyncStatistics syncStats;
    syncStats = syncAgent.Synchronize();
    // After synchronizing the data, refill the
    // table in the dataset.
    this.customersTableAdapter.Fill(this.northwindDataSet.Customers);
    
  5. 同步處理完成後請加入程式碼,將同步處理後本機資料庫中的資料,重新填入應用程式中的資料來源。

請參閱

工作

逐步解說:建立偶爾連接的應用程式

HOW TO:在應用程式中設定資料同步處理

概念

偶爾連接的應用程式概觀

SQL Server Compact 4.0 和 Visual Studio