Introducing LINQ with XML and Databases
(Page 1 of 4 )
In the previous part of the article, we looked at the new syntax provided by LINQ through exploring some simple scenarios involving an array of Person objects. We extracted certain elements, ordered them and grouped them according to the specific scenario. LINQ, however, is not limited to simple arrays. While arrays are useful for illustrating the basic principles behind LINQ, LINQ is much more powerful and can work with other sources of data, such as XML files or relational databases. In this article, we'll explore, in brief, the usage of LINQ with XML and with SQL databases.
To access a particular type of datasource, something called a provider is used. Providers basically provide a link between LINQ and the appropriate datasource. In the last article, we used LINQ to query an array. The LINQ to Objects provider was involved in this, though knowledge of the provider was, of course, unnecessary to the task. .NET also comes with other providers, such as LINQ to XML and LINQ to SQL, which we'll be using in this article, and it's possible to create providers; a number of third party providers are available.
The Adventure Works Cycles Sample Database
We'll start by examining LINQ to SQL. To do this, we'll use the Adventure Works Cycles sample database. It's available for download at CodePlex:
http://www.codeplex.com/MSFTDBProdSamples/Release/ProjectReleases.aspx?ReleaseId=4004
We'll use the lighter version of the database since the full database is very complex, and we're not ready to add more complexity at this stage. The lighter version is called AdventureWorksLT. Download the appropriate installer and run it. The installer creates the database at X:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorksLT_Data.mdf.
Create a new project in Visual Studio using the Console Application template. Then, add the Adventure Works database as a datasource. We're now ready to begin using LINQ to SQL.
Adding LINQ to SQL Classes
Now, we need a way to map the information contained in the relational database to classes that we can use. It's possible to do this manually, but this is unnecessary since Visual Studio will do it for us with just a few clicks. Add a new item to the project. Notice how there is a LINQ to SQL Classes item. This is what we need. Name the item AdventureWorks and then add it.
We're now able to visually create the classes that we need. Upon creating the new item, you should see the Object Relational Designer. Open the Database Explorer and view the list of tables included in the Adventure Works database. We're going to be working with customers. Drag the Customer table over to the main pane of the Object Relational Designer. Visual Studio will automatically create the proper class. You're able to view the source inside of the AdventureWorks.designer.cs file, but we're only interested in the visual representation right now. In the Designer, you should see a box representing the Customer table containing a list of properties that correspond to database fields.
We're almost ready to begin querying the database. The final preparation step is to create an instance of the data context, which will allow us to use the class we just created, along with any other classes we may choose to create later:
AdventureWorksDataContext db = new AdventureWorksDataContext();
Next: Querying the Database >>
More .NET Articles
More By Peyton McCullough