My First Microsoft Sync Framework Application, Page 2
Now click OK. You will notice Visual Studio performing actions on your behalf while it sets up the local cache and sync file and performs synchronization. When you receive a prompt to generate the SQL scripts, click OK. These are the scripts that update the SQL Server with the necessary changes to enable sync functionality.
Listing 1.5 Choose OK to Generate SQL Scripts.
Listing 1.6 VS synchronizes the SQL Compact database with the AdventureWorks database
The following References are added to the project during this process:
Finally, the "Data Source Configuration Wizard" dialog will be opened to allow us to create the datasets that will be used in the application. Select all the Tables and click the Finish button.
Click here for larger image
Listing 1.7 Set up datasets
So far, Visual Studio has enabled us to perform the following actions, all through dialog configuration without writing a single line of code:
- Configuring the selected SQL Server database for change tracking
- Creating an SQL Compact database and schema for the local data cache
- Creating the SQL Scripts and SQL Undo Scripts for data synchronization
- Creating datasets for use in the application
We are now going to use a few more powerful features of Visual Studio to create our form's controls and link them back to the database. In the Data Sources window, change the Sales_SalesPerson table to use the Details view and TerritoryID to use the combo box. Now drag the Sales_SalesPerson table onto the form. Once the controls are generated, drag the Sales_SalesTerritory table to the Territory ID combo box. When complete, you should have a form that looks similar to the one below when running.
Listing 1.8 The running application
We now have a form that allows user to view and update salesperson information in the local data cache. Next, we will expose a way for the user to initiate synchronization. Since all views, inserts, updates, and deletes are happening locally on the SQL Compact database, we still need a way to receive changes from the AdventureWorks database.
First, we will add a button to the toolbar. Near the save button on the top, click on the Add ToolStripButton dropdown and select Button. Double click the new button to generate the click event handler. Here, we will initiate synchronization. You must be thinking, "Finally I have to write some code." However, Microsoft has us covered again. Double click on the AWLocalDataCache.sync file in Solution Explorer to reopen the "Configure Data Synchronization" dialog. Located in the bottom right, above the OK and Cancel buttons, is a link that reads "Show Code Example " Click this button to view the code that will initiate synchronization.
Click here for larger image
Listing 1.9 Visual Studio offers you the code for synchronizing the local cache
Click Copy Code to the Clipboard, click Close, and cancel out of the dialogs. Now paste the code into the event handler for the button we just created. As the handy "TODO" in the provided code instructs, we will want to add a simple reload/merge to refresh the form with any new data pulled from the server.
// TODO: Reload your project data source from the local database (for example, call the TableAdapter.Fill method). this.adventureWorksDataSet.Sales_SalesPerson.Merge(this.sales_SalesPersonTableAdapter.GetData());