Microsoft 9GD00001 Computer Accessories User Manual


 
200 Microsoft Visual Studio 2010: A Beginner’s Guide
Handling Data with LINQ to SQL
The LINQ to SQL provider allows you to communicate with SQL Server databases. There
are many other types of providers, such as LINQ to Entities for generic databases (which
includes SQL Server), LINQ to XML for XML data sources, and LINQ to Oracle for
Oracle databases. The preceding section showed you how to use the in-memory provider,
LINQ to Objects. However, LINQ to SQL is the easiest database provider to learn and
ships with VS. Once you learn LINQ to SQL, the journey to other providers is easier.
The following sections will show you how to set up LINQ to SQL, perform queries, and
modify data.
Setting Up LINQ to SQL
Setting up LINQ to SQL involves running the LINQ to SQL Wizard and adding classes
and methods. Behind the scenes, LINQ to SQL generates code, saving you a lot of work.
The result of setting up LINQ to SQL is that you will have a data model, which is an
environment with classes that you can use to query and modify database data and call
methods for invoking stored procedures.
Before setting up LINQ to SQL, you’ll need to create a project (a Console project
for the purposes of this chapter). See Chapter 5 if you need a refresher on how to set up
a Console project. Select Add | New Item, select LINQ to SQL Classes, name the file
MyShop.dbml, and click Add. This will show you the LINQ to SQL Designer, with two
surfaces for classes and methods. Figure 7-11 shows the LINQ to SQL Designer with
a couple of classes and a method.
Figure 7-11 The LINQ to SQL Designer