LINQ to SQL
Lesson 37 provided an introduction to LINQ to Objects. This lesson gives a brief introduction
to another of the LINQ family of technologies: LINQ to SQL.
LINQ to SQL lets you use queries similar to those provided by LINQ to Objects to manipulate
SQL Server databases. It uses a set of classes to represent database objects such as tables and
records. The classes provide intuitive methods for adding, modifying, deleting, and otherwise
manipulating the records.
In this lesson you learn the basics of LINQ to SQL. You learn how to make LINQ objects rep-
resenting a SQL Server database and how to add records to the database. You also learn how
to perform queries similar to those described in Lesson 37 to filter and sort data taken from
the database.
The basic steps for making a LINQ to SQL application are connecting to the database, making
LINQ to SQL classes, and writing code to manipulate the data.
Note that the programs and techniques described
in this chapter demonstrate only very simple uses of
LINQ to SQL. For more information, search the Web.
Microsoft’s “Language-Integrated Query (LINQ)
page at
msdn.microsoft.com/library/bb386976.
aspx
provides a good starting point for learning more
about LINQ to SQL.
CONNECTING TO THE DATABASE
The first step in creating a LINQ to SQL program is
connecting to the database. Create a Windows Forms
application as usual. Then open the Server Explorer
shown in Figure 38-1. Use the View menu’s Server
Explorer command if you cant find it.
FIGURE 381
38
596906c38.indd 423 4/7/10 12:35:04 PM
424
LESSON 38 LINQ to SQL
To run most of the programs described in this chapter, you need to have SQL
Server installed on your computer. The Visual Studio installation software
comes with SQL Server (at least the versions I’ve seen) or you can download the
free SQL Server Express edition at
www.microsoft.com/express/Database.
You should probably download the management tools, too. You may find SQL
Server Management Studio particularly useful for building databases.
Unfortunately there isn’t room here to say too much about SQL Server and how
to use it. You’ll have to rely on the Web or get a book about SQL Server to do
much with it.
To make using the examples described in this lesson a bit easier, the download
includes a program named BuildCustomerDatabase. This program connects to
your SQL Server instance, deletes the database named CustomerDatabase if it
exists, and creates a new CustomerDatabase containing a few records for the
examples described in this lesson to use.
Obviously don’t run this program if you already have a database named
CustomerDatabase that you want to preserve because that database will
be destroyed.
Click the Connect to Database button (third from the left at the top in Figure 38-1) to display the
Add Connection dialog shown in Figure 38-2.
Initially the Add Connection dialog may have some type of database other than SQL Server selected.
The dialog shown in Figure 38-2 is ready to connect to a Microsoft Access database. To switch to
SQL Server, click the Change button to display the dialog shown in Figure 38-3.
FIGURE 382
FIGURE 383
596906c38.indd 424 4/7/10 12:35:05 PM
Connecting to the Database
425
Select the Microsoft SQL Server entry and click OK. When you return to the Add Connection dia-
log, it should look like Figure 38-4.
FIGURE 384
Enter your server name in the indicated textbox. If you are running SQL Server Express Edition,
follow the server’s name with \SQLEXPRESS as shown in the figure.
Enter the name of the database on the server (I entered CustomerDatabase in Figure 38-4) and
click OK.
In the dialog shown in Figure 38-4, if you enter the name of a database that
doesn’t exist on the server, Visual Studio tells you that the database doesn’t
exist and asks if you want to create it. If you click Yes, you can use the Server
Explorer to build the database. The Server Explorer doesn’t provide as many
features as a database management tool such as SQL Server Management
Studio, but its handy if you don’t have easy access to those tools.
If you go back to Figure 38-1, you can see the Server Explorer with the GOTHMOG\SQLEXPRESS
server’s CustomerDatabase expanded to show its single table, Customers, and its columns.
596906c38.indd 425 4/7/10 12:35:05 PM

Get Stephens' C# Programming with Visual Studio® 2010 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.