October 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Language Integrated Query (LINQ) to SQL

  • April 28, 2009
  • By Mark Strawmyer
  • Send Email »
  • More Articles »
Welcome to this installment of the .NET Nuts & Boltscolumn. The prior two articles have been an introduction toLINQ and LINQ to XML. This article will focus on anothervariation of LINQ, which is LINQ to SQL. LINQ to SQL allowsyou to query and manipulate objects associated withrelational database tables and is another valuable part ofthe LINQ family.

Introducing LINQ to Relational Data

LINQ introduced the concept of a unified data accesstechnique to eliminate challenges of accessing data from adisparity of sources. LINQ has been extended to include theability to access relational data through LINQ to SQL andLINQ to Entities. LINQ to SQL offers a direct mapping tothe Microsoft SQL Server family of databases and allows youto query and manipulate objects associated with databasetables. LINQ to Entities offers a more flexible mapping torelational data stored in other databases beyond just SQLServer. You build against a conceptual schema that ismapped to an actual schema. The remainder of this articlewill concentrate on LINQ to SQL.

LINQ to SQL Object Model

LINQ to SQL allows you to query and manipulate objectsassociated with database tables. Creating an object modelinvolves creating a typed object that represents thedatabase connection along with classes that map to databaseentities. The following list of objects are involved:
  • DataContext - A base class that is a disposable type that gets extended to represent a strongly typed connection to the database.
  • Classes and Collections - Represent objects and collection of objects that map to database entities.
  • Methods - Are used to extend classes or to represent stored procedure calls.

There are a few different ways to create an object model.The two most straight forward approaches are to use theprovided designer within Visual Studio 2008 to auto-generatethe objects and mappings or adding attributes to existingobjects. For this example I downloaded the Northwind sample databaseand loaded it in my SQL Server so that I could retrieve datafrom it for testing. I created a new solution and added aconsole application. I then added a LINQ to SQL data classto the console application. I used the Server Explorer tosetup a new connection to the Northwind database my localSQL Server, and then dragged all of the tables from the ServerExplorer on to the design surface for the data class.Finally I right clicked on the design surface and chose"Layout Diagram" to auto arrange all of the newly addedtables and relationships. Refer to Figure 1 for an exampleof the completed design surface.



Click here for larger image

Figure 1 - LINQ to SQL Data Class Design Surface

Test Driving LINQ to SQL through Examples

Now that we've covered the background let's use a couple ofexamples of LINQ to SQL. Our examples will demonstrate theuse of the DataContext object along with an example ofquerying the Northwind sample database using expressions.



Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel