Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Work with Data

Data is the domain of the business developer. It makes sense then that the number one tool and framework for business development continues to provide new and better ways of accessing and exposing that data. Data access is everywhere inside Visual Studio and the .NET Framework. Here we highlight some of the new things you will encounter in the 2008 version.

Design Your Data

A typed dataset allows you to create a .NET class that is based on your database table semantics but works as a dataset behind the scenes. Typed datasets have been part of Visual Studio for a while now. You can auto-generate them based on table schemas and edit the code used to query, update, insert, and delete data.

Visual Studio 2008 provides additional design-time support for typed datasets. You can create a typed dataset file (.xsd) and use the Toolbox and Server Explorer to build the dataset. Figure 1.40 shows an example of this. Tables from the Server Explorer were added to the design surface to build the dataset.

Figure 1.40

Figure 1.40 Building a typed dataset with the dataset designer.

In addition, the typed dataset in 2008 now supports hierarchical updates. This feature allows you to save data in multiple, related tables through the typed dataset. In Figure 1.40, this would mean you could edit both the employee details and the related territories and update as a single process.

Map Objects to Relational Data

Most business applications rely on data that is stored in relational databases. Databases have a well-known structure of tables, columns, and relationships. This structure makes for a nice storage and reporting mechanism. However, there is often a mismatch between the needs of an object-oriented, .NET developer and the database technology. The object developer thinks in terms of objects, properties, methods, relationships, encapsulation, and the like. A lot of time is spent converting data from a database into an object-oriented structure and back again. In addition, developers are asked to write code in Structured Query Language (SQL) and .NET.

LINQ, which we introduce in Chapter 3, is a technology that helps bridge this gap. It provides a means to work with database records as objects. It also allows you to write your queries using .NET (C# and VB). On top of LINQ is an object relational (O/R) designer for SQL Server called LINQ to SQL. With it, you can design and build .NET classes based on database schemas. In this way, you can quickly take database concepts and convert them to object-oriented ones.

You use a Visual Studio template to create a LINQ to SQL class. You can add such a class to any project type (web, Windows, class library, and so on). Figure 1.41 shows the Add New Item dialog with the LINQ to SQL Classes template selected.

Figure 1.41

Figure 1.41 Creating a new LINQ to SQL class.

You can then use the O/R Designer to build your LINQ class. You first use Server Explorer to open a connection to the database with which you are working. You can then drag and drop tables from the database onto the design surface. The designer understands (and enforces) the table relationships. It maps tables to classes and columns to properties.

Figure 1.42 shows an example of the designer. Here there is a customer class with an association link to the customer's orders. Notice also the methods listed on the right side of the diagram. These were created by dragging stored procedures from Server Explorer to the designer. A developer may now work with these classes and methods in a similar manner as they would with other .NET objects.

Figure 1.42

Figure 1.42 Mapping database relationships to objects.

Build Occasionally Connected Applications

Many applications written these days require access to data both on- and offline. For example, you might need report data or customer service records when traveling or at a customer site. You cannot always rely on being connected. Developers have been dealing with this issue in various ways. The latest version of Visual Studio provides data synchronization services to provide a common, easy solution to this difficult problem.

Data synchronization allows you to synch and cache data between a central database and a user's client system. The cache is a version of SQL Server Compact Edition (CE). Microsoft is already taking advantage of these services in Vista, Office, and even their Zune software. You too can take advantage of these services in your applications.

Looking back at Figure 1.41, you will notice a template called Local Databa.... This template is actually called Local Database Cache. With it, you create a .sync file to configure how synchronization should happen between a local data store and a server. Opening the local database cache file in Visual Studio opens the Configure Data Synchronization dialog. Here you can set up tables that should be cached on a client, set up the server database, and configure the client database information. Figure 1.43 shows an example of this dialog.

Figure 1.43

Figure 1.43 Configuring data synchronization.

The Cached Tables area of the Configure Data Synchronization dialog allows you to define which tables will be synchronized. Figure 1.44 shows an example of adding tables to this list. Here you configure how synchronization happens for each table. Synchronization services are discussed further in Chapter 18.

Figure 1.44

Figure 1.44 Adding and configuring table synchronization.

  • + Share This
  • 🔖 Save To Your Account