1. Synchronization Services for ADO.NET (Devices) Sample This sample is designed to help you understand how Synchronization Services for ADO.NET can be used to synchronize data between a server and a device. To provide feedback about this sample or Synchronization Services, use the Synchronization Services Forum. For an example of a walkthrough that demonstrates device synchronization, see “Walkthrough: Creating an Occasionally Connected Smart Device Application” in the Visual Studio 2008 Service Pack 1 (SP1) documentation. 2. Software Requirements for this Sample The sample requires the following software to be installed and configured:  SQL Server 2005 or SQL Server 2008  Internet Information Services (IIS) 6.0 or later version with ASP.NET  Visual Studio 2008 Professional SP1, installed with the Smart Device Programmability option  SQL Server Compact 3.5 Service Pack 1 (SP1) The sample requires device components, desktop components, and Visual Studio tools. The desktop components include Synchronization Services for ADO.NET 1.0 SP1 for the desktop, which is required for the server.  Synchronization Services for ADO.NET 1.0 (devices) Run the MSI to copy the device binaries and cab files to the following folder: \Program Files\Microsoft Synchronization Services\ADO.NET\v1.0\Devices.  ActiveSync for Windows XP or the Windows Mobile Device Center for Windows Vista. One of these technologies is required for communication between the device and the development computer, which acts as the server. 3. Architecture for Device Synchronization Synchronization Services supports both 2-tier and N-tier topologies for desktop synchronization. However, because of footprint considerations on the device, Synchronization Services supports only N-tier topologies for device synchronization. Configuring synchronization for a device is similar to configuring an N-tier topology for desktop synchronization. The architecture is shown in the following illustration. In an N-tier architecture for devices, synchronization components are used as follows. References to each of these DLLs in the sample are discussed later in this document. The server synchronization provider (DbServerSyncProvider) resides on the server or another tier. The provider is contained in Microsoft.Synchronization.Data.Server.dll. Use the DLL that is included with SQL Server Compact 3.5 SP1 or Visual Studio 2008 SP1. The client synchronization provider (SqlCeClientSyncProvider) and the synchronization agent (SyncAgent) both reside on the device. The agent is contained in Microsoft.Synchronization.Data.dll and the provider is contained in Microsoft.Synchronization.Data.SqlServerCe.dll. Use the device versions of the DLLs. These are available from this Microsoft Web site. Communication between the server synchronization provider and the synchronization agent is handled by the server synchronization provider proxy (ServerSyncProviderProxy) and a service. The provider proxy is contained in Microsoft.Synchronization.Data.dll. Synchronization Services does not specify the kind of service that you must use. However, applications typically use a Web service or a Windows Communication Foundation (WCF) service. 4. Sample Projects and Files To become familiar with the sample, open GBASyncServicesForDevices.sln by using Visual Studio 2008. Browse through the projects and files in the solution. The code for this sample is a combination of code generated by the Local Database Cache designer, other Visual Studio designers, and some custom synchronization code. For information about the Local Database Cache, see the Visual Studio 2008 documentation. For information about writing synchronization code, see the Synchronization Services documentation. In addition to the projects listed in this section, you will add a Web site to the solution. This site hosts the service that enables communication between the server and the device. GBAServerSyncProvider This project contains the server side configuration code for Synchronization Services, including the Transact-SQL commands to retrieve changes from and apply changes to the server database.  GBACache.Server.sync and GBACache.Server.Designer.cs o These files were generated by using the Local Database Cache designer in Visual Studio. This designer generates the client and server synchronization components. The files were originally generated in the DesktopClient project. Therefore, if you double-click the .sync file, you are redirected to this project. For more information, see GBADesktopClient in this document.  GBACache.Server.cs (nested below the GBACache.Server.sync file) o Includes partial classes that could be used to handle business rules and conflict scenarios.  GBABuilderServerSyncProvider.cs o A sample of how to configure the server provider by using the SyncAdapterBuilder. The synchronization adapter builder is modeled after the command builder in ADO.NET. The adapter builder creates the commands to select changes from and apply changes to the server database. For production applications, we recommend that you specify these commands manually and that you use stored procedures for database access. Stored procedures can help to improve the performance and security of an application.  Properties.Settings o Includes SQL Server connection string information. GBADeviceClient This project contains the client-side configuration code for Synchronization Services.  Sync folder o ClientSyncAgent.cs The synchronization agent code was hand-coded for this simple client configuration. In an application, you could also use the code that is generated by the Local Database Cache designer. The synchronization agent communicates directly with the client synchronization provider and uses a Web service to communicate with the server synchronization provider. o ServerSyncProviderProxy.cs Synchronization Services uses a ServerSyncProviderProxy object to abstract the differences between Web Services, WCF or other transports that require their own base class. o ConfiguredSyncWebServiceProxy.cs This file was created by adding a Web Reference to the GBADeviceClient project. Web References in Visual Studio create proxies for all types returned by a service. This creates duplicate types for Synchronization Services. To work around this limitation, the proxy types are removed from the generated file, and a Using statement is added to redirect all type names to the Microsoft.Syncronization.Data namespace. The reference.cs file created by Visual Studio was edited and renamed to ConfiguredSyncWebServiceProxy.cs so that the file is not overwritten by Visual Studio.  GBAppraiseDemo.sdf o The SQL Server Compact database file, which was generated by the Local Database Cache designer. For this sample, the file is not deployed to the device; it is created on the first run of the application. The Copy to Output Directory value has been changed to Do not copy, which follows the best practice of not deploying the data and the schema with the application, but rather creating it on the first run.  GBAppraiseDemoDataSet.xsd o A typed dataset that is used to bind data from the database to the UI controls. Synchronization Services does not require a dataset to interact with data within SQL Server Compact. Typed datasets are simply used for rapid form design and could be substituted with a SqlCeResultSet object.  GBAppraiseUI.cs o The main form of the project. o As you scroll through the list of “Jobs” displayed in the top grid, the additional elements of the Job table are displayed below. o The functionality of the form is limited to marking a job complete or incomplete by using the left menu button. The BindingSource.CurrentChanged event is used to update the text of the left menu button. o The right menu button [Options] enables you to synchronize the device or exit the application.  Program.cs o Includes the initialization code to ensure that a database exists on the device.  Settings.cs o Includes the local connection string and Web service URL. The Web service URL should be updated to reflect the server name and virtual directory that you specify when you add a new Web service later in this document.  SynchronizingProgress.cs o A UI that displays a wait cursor during the initial synchronization session. Service.cs This file defines a Web service with four methods that are used to communicate with the server synchronization provider. This file is used when you add a new Web service later in this document. Solution Items The Solution Items folder contains the CreateGBAppraiseDB.sql script file and the GBADesktopClient project. CreateGBAppraiseDB.sql This file contains a Transact-SQL script that creates the GBAppraiseDemo sample database and the gbauser login on the instance of SQL Server that you connect to. The database contains a set of base tables and tracking tables that are used in the sample application. GBADesktopClient The Local Database Cache designer cannot be started from a smart device project. The GBADesktopClient project was created so that N-tier code could be generated and then used by the GBAServerSyncProvider and GBADeviceClient projects. 5. Configuring and Running the Sample Configuring and running the sample involves the following steps, which are described in more detail in this section of the document: 1. Configuring the database 2. Configuring settings for your environment 3. Building the GBAServerSyncProvider project 4. Adding an ASP.NET Web service project 5. Building the GBADeviceClient project 6. Deploying and running the application Configuring the database The sample database contains tables and data that are downloaded to the client database during the first synchronization session. To configure the sample database: 1. Ensure that the instance of SQL Server that you will use for the server database is configured for mixed mode authentication. For more information, see SQL Server Books Online. Because the sample uses a Web service, we do not assume that you can connect to SQL Server by using Windows Authentication. 2. Execute the script CreateGBAppraiseDB.sql to create the GBAppraiseDemo sample database. By default, the script creates a user named “gbauser” with a password of “gbauser”. You can modify the script to use a different name and a strong password. If you do so, ensure that you change the value of the ServerGBAppraiseDemoConnectionString property in the GBAServerSyncProvider project. Configuring settings for your environment There are several settings, such as name of the server that must be configured. #error compile directives are included in the code to help you understand where changes are required. Files that require changes include:  GBAServerSyncProvider\Properties\Settings.Designer.cs  GBADeviceClient\Settings.cs  GBADesktopClient\Properties\Settings.Designer.cs Building the GBAServerSyncProvider project 1. Ensure that references are set to the desktop version of the following Synchronization Services DLLs:  Microsoft.Synchronization.Data.dll: in \Program Files\Microsoft Synchronization Services\ ADO.NET\v1.0  Microsoft.Synchronization.Data.Server.dll: in \Program Files\Microsoft Synchronization Services\ADO.NET\v1.0 2. Build the GBAServerSyncProvider project, which creates the GBAServerSyncProvider.dll in the GBAServerSyncProvider\bin\debug folder. Adding an ASP.NET Web service project To add an ASP.NET C# Web service project: 1. Right-click the solution, point to Add, and then click New Web Site. 2. In the Add New Web Site dialog box:  Select ASP.NET Web Service.  From the Location menu, select HTTP.  From the Language menu, select Visual C#.  Enter the address of the Web site that will be created. Ensure that the address includes a computer name (not localhost) and that the address matches the settings in GBADeviceClient\settings.cs and GBADesktopclient\Properties\settings.designer.cs. 3. Replace the contents of the Service.cs file in the project with the Service.cs file that is provided in the sample directory. 4. Add references to GBAServerSyncProvider.dll and the desktop version of the following Synchronization Services DLLs:  GBAServerSyncProvider.dll: in GBAServerSyncProvider\bin\debug  Microsoft.Synchronization.Data.dll: in \Program Files\Microsoft Synchronization Services\ ADO.NET\v1.0  Microsoft.Synchronization.Data.Server.dll: in \Program Files\Microsoft Synchronization Services\ADO.NET\v1.0 5. Build the Web site. 6. Test the Web service by entering the address of service.asmx into a Web browser, such as http://mycomputer/gbappraise/service.asmx. You should see the following content in the browser. The server provider that is defined in GBAServerSyncProvider is now exposed as a Web service. Build the GBADeviceClient project To build the GBA Device Client project: 1. Ensure that references are set to the device version of the following SQL Server Compact and Synchronization Services DLLs:  System.Data.SqlServerCe.dll: in \Program Files\Microsoft SQL Server Compact Edition\v3.5\Devices  Microsoft.Synchronization.Data.dll in \Program Files\Microsoft Synchronization Services\ADO.NET\v1.0\Devices  Microsoft.Synchronization.Data.SqlServerCe.dll in \Program Files\Microsoft Synchronization Services\ADO.NET\v1.0\Devices. 2. Build the project. Run the Application To run the application: 1. Ensure that Windows Mobile Device Center (Windows Vista) or ActiveSync is configured to allow DMA connections. For more information, see the documentation for each technology. 2. In Visual Studio, press F5 to deploy and debug the application. 3. In the Deploy GBADeviceClient dialog box, select USA Windows Mobile 5.0 Pocket PC R2 Emulator, and then click Deploy. 4. From the Tools menu, click Device Emulator Manager (DEM). The DEM window appears with a list of available emulators. 5. In the DEM window, right-click the connected device and click Cradle. Windows Mobile Device Center or ActiveSync connects to the cradled device emulator. If Windows Mobile Device Center or ActiveSync offers to set up the device, click Connect without setting up your device (Windows Vista) or Cancel. After the application is deployed, a sample application dialog box appears in the device emulator. 6. Click OK to initialize the SQL Server Compact database on the device. After the database is initialized, the application displays a list of records in the top half of the screen, and details for each record in the bottom half. Note: If an exception is thrown at this point, it is likely that the device cannot connect to the Web server, or that the Web server cannot connect to the database. Ensure that users have adequate permissions to make the required connections. 7. In the sample application, select a record and click Mark Complete or Mark Incomplete at the bottom of the screen. 8. In Visual Studio or SQL Server Management Studio, connect to SQL Server on the server computer and make a change to a record in the sample database. 9. In the sample application, click Options and then click Sync Now. The changes you made on the device and on the server are synchronized and both nodes now have the same data.

© 2008 Microsoft Corporation. All rights reserved. This sample (including any documentation provided with the sample) is provided AS IS, WITHOUT WARRANTY OF ANY KIND, is for informational purposes only, and is subject to the terms specified at http://www.microsoft.com/info/cpyright.htm.