Share via


Occasionally Connected Applications Overview

An occasionally connected application is an application that uses data from a remote database but that might not always have access to the remote database. Occasionally connected applications are also used when access is continuously available but it might not be efficient to continuously query the remote database for data that is rarely changed, such as a list of states or product categories. In these situations, applications can use data from a local database that is located on the client and periodically synchronize the data with the remote database on the server.

In other words, if your application requires data from a remote database that does not have to be continuously retrieved from the database, you can use a local database cache to store the data on the client computer or directly inside your application. For example, consider an inventory application that uses data from several tables in a database. The number of items in stock for any individual part is an important piece of data that continually changes, so the application should always reflect the current values in the database if possible. However, your application also displays a list of valid shipping companies that rarely changes. These valid shipping companies are stored in the shippers table and do not have to be retrieved every time data is queried from the database. By storing this shippers table in a local database cache, you can decrease the number of unnecessary roundtrips that your application has to make to the remote database. So, in addition to using a local database cache for applications that have occasional connectivity, consider storing data that changes infrequently, or that changes on a known schedule, in a local database cache.

You can configure caching to store individual tables, sets of tables, or specific records queried from a table (for example, only customers who have a specific sales representative).

The synchronization of data between client and server databases is facilitated by Microsoft Synchronization Services for ADO.NET (the Microsoft.Synchronization.Data namespace). You can configure synchronization visually in Visual Studio by using the Configure Data Synchronization dialog box.

Starting in Visual Studio 2008 SP1, in addition to the Configure Data Synchronization dialog box, you can also use the Data Source Configuration Wizard to configure synchronization. During configuration of a typed dataset, select the option to Enable local database caching on the Choose Your Database Objects page of the wizard. For more information, see Walkthrough: Creating an Occasionally Connected Application by Using the Data Source Configuration Wizard.

Note

In smart device projects, the synchronization designer cannot be started from the Data Source Configuration Wizard. For more information, see Occasionally Connected Applications (Devices).

In addition, support for the change tracking feature in SQL Server 2008 has been added for Visual Studio 2008 SP1. You can enable SQL Server 2008 change tracking when you configure synchronization from the Configure Data Synchronization dialog box or the Data Source Configuration Wizard. For more information, see How to: Configure Data Synchronization to Use SQL Server Change Tracking.

Synchronization Services API (Microsoft.Synchronization.Data)

Microsoft Synchronization Services for ADO.NET is designed for occasionally connected applications. It lets you synchronize data from disparate sources over two-tier, n-tier, and service-based architectures. Instead of just replicating a database and its schema, the Synchronization Services API provides a set of components to synchronize data between data services and a local store. Applications are increasingly used on mobile clients, such as portable computers and devices, that do not have a consistent or reliable network connection to a central server. It is important for these applications to work with a local copy of data on the client. Equally important is the requirement to synchronize the local copy of the data with data on a central server when a network connection is available. The Synchronization Services API, which is modeled after the ADO.NET data access APIs, gives you an intuitive way to synchronize data. It makes creating applications for occasionally connected environments a logical extension of creating applications for which you can depend on a consistent network connection.

Note

In smart device projects, 2-tier architecture is not supported. For more information, see Occasionally Connected Applications (Devices).

Installing the Synchronization Services API and Documentation

The Synchronization Services API is not part of the .NET Framework. It is installed as part of the SQL Server Compact 3.5 installation. (By default, SQL Server Compact 3.5 is installed when you install Visual Studio.) By default, the Synchronization Services documentation is not installed. Therefore, it will be available only if Help is configured for online use. To install the Synchronization Services documentation for local use, you must first download the Help collection from the Microsoft Synchronization Services for ADO.NET Books Online Web site.

Incorporating Data Synchronization into Applications by Using Visual Studio

To incorporate data synchronization into your application, Visual Studio provides a Local Database Cache template. Local Database Cache is available as a new project item template. (On the Project menu, click Add New Item.) For more information, see How to: Configure Data Synchronization in an Application.

Note

The Local Database Cache template cannot be directly added to a smart device project. Instead, you must add the Local Database Cache to the middle-tier WCF or Web service project and then split the generated synchronization components into the smart device project. For more information, see Occasionally Connected Applications (Devices).

Starting in Visual Studio 2008 SP1, in addition to the Configure Data Synchronization dialog box, you can also use the Data Source Configuration Wizard to configure synchronization. During configuration of a typed dataset, select the option to Enable local database caching on the Choose Your Database Objects page of the wizard. For more information, see Walkthrough: Creating an Occasionally Connected Application by Using the Data Source Configuration Wizard.

Adding a Local Database Cache opens the Configure Data Synchronization dialog box. You use this dialog box to provide specific information about the client and server in order to set up the required synchronization components. The local database cache uses a SQL Server Compact 3.5 database on the client to store data locally. You can use an existing SQL Server Compact 3.5 database as the local database cache. If you do not yet have a local database, you can use the Configure Data Synchronization dialog box to create a new local database. To create a new local SQL Server Compact 3.5 database, you can set the Client connection (in the Configure Data Synchronization dialog box) to create a new SQL Server Compact 3.5 database based on the tables selected from the Server connection.

Note

The Configure Data Synchronization dialog box enables you to configure Microsoft Synchronization Services for ADO.NET for download scenarios only. This means that after you configure data synchronization by using this dialog box, calling Microsoft.Synchronization.Data.SyncAgent.Synchronize will only update the local database with changes found on the remote database. Changes made to the data on the local database will not be uploaded to the remote database. After you configure data synchronization by using the Configure Data Synchronization dialog box, you can programmatically enable uploads (bidirectional synchronization) during synchronization. For more information, see How to: Configure a Local and Remote Database for Bidirectional Synchronization.

Configuring the Remote Database for Synchronization

For data synchronization to work successfully, there are some additions that are required to each table on the remote database with which you want to synchronize. The Configure Data Synchronization dialog box creates SQL scripts to run against the remote database to create the necessary objects (the objects listed in the table below). All SQL scripts that are created by the synchronization designer are saved to a SQLScripts folder in your project.

Note

The SQL scripts generated by the Configure Data Synchronization dialog box are run by default. You can choose whether the scripts are generated and executed by setting the Script Generation options in the Configure Tables for Offline Use dialog box.

The default behavior of the Configure Data Synchronization dialog box is to automatically run the scripts and update the remote database when the dialog box is closed. Clear the Execute scripts when this dialog is closed check box if you do not want to automatically run the scripts. Additionally, if the remote database already has the required tracking columns, triggers, and deleted-items table, no scripts will be generated. In other words, if no changes are required to the remote database, no scripts are created.

The following table lists the items required on the remote database and provides an explanation for each:

Additions to each synchronized table on the remote database

Explanation

LastEditDate column

This column should be a DateTime or TimeStamp on each table being synchronized. It is compared to the LastEditDate column on the client to identify records that have been modified on the server since the last synchronization call.

CreationDate column

This column should be a DateTime or TimeStamp on each table being synchronized. It is compared to the CreationDate column on the client to identify records that have been added to the server since the last synchronization call.

deleted items table (TableName_Deleted)

Items are moved to this table when they are deleted from the table on the database server. This is done to identify records deleted from the server since the last synchronization call. Each table being synchronized requires a deleted items table to track records deleted from the remote table.

DeletionTrigger (TableName_DeletionTrigger)

This trigger runs every time that a record is deleted from the server database table. Deleted records are moved to the deleted items table. They are moved to the deleted items table because records in the client database that are not in the server database might be treated as new records and added back to the server. Synchronization Services checks the deleted items table to determine that it should delete a deleted record from the client database instead of adding it back to the server database.

InsertTrigger (TableName_InsertTrigger)

This trigger populates the CreationDate column with the current date and time when new records are added.

UpdateTrigger (TableName_UpdateTrigger)

This trigger populates the LastEditDate column with the current date and time when existing records are modified.

Starting the Synchronization Process from an Application

After providing the required information in the Configure Data Synchronization dialog box, add code to your application to initiate synchronization. It is important to understand that synchronizing data updates the local database, not the table in the dataset or any other object in your application. Remember to reload your application data source with the updated data from the local database. For example, call the TableAdapter.Fill method to load your dataset's data table with the updated data from the local database.

Add the following code to your application where you want to initiate the synchronization process:

' Call SyncAgent.Synchronize() to initiate the synchronization process. 
' Synchronization only updates the local database, not your project's data source. 
Dim syncAgent As LocalDataCache1SyncAgent = New LocalDataCache1SyncAgent()
Dim syncStats As Microsoft.Synchronization.Data.SyncStatistics = syncAgent.Synchronize()

' Add code here to refill your application's data source 
' with the updated data from the local database.
// Call SyncAgent.Synchronize() to initiate the synchronization process. 
// Synchronization only updates the local database, not your project's data source.
LocalDataCache1SyncAgent syncAgent = new LocalDataCache1SyncAgent();
Microsoft.Synchronization.Data.SyncStatistics syncStats =
syncAgent.Synchronize();

// Add code to refill your application's data source 
// with the updated data from the local database.

See Also

Tasks

How to: Configure Data Synchronization in an Application

Walkthrough: Creating an Occasionally Connected Application

How to: Configure a Local and Remote Database for Bidirectional Synchronization

Walkthrough: Creating an Occasionally Connected Smart Device Application

Concepts

SQL Server Compact 3.5 and Visual Studio

Occasionally Connected Applications (Devices)

Other Resources

Accessing Data (Visual Studio)

Change History

Date

History

Reason

July 2008

Added a paragraph about incorporating data synchronization from the Data Source Configuration Wizard.

SP1 feature change.

July 2008

Added a paragraph about enabling SQL Server change tracking when configuring data synchronization.

SP1 feature change.

July 2008

Added notes about synchronization in smart device projects.

SP1 feature change.