.NET
  Home arrow .NET arrow Introducing LINQ with XML and Databases
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Introducing LINQ with XML and Databases
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-06-16

    Table of Contents:
  • Introducing LINQ with XML and Databases
  • Querying the Database
  • LINQ to XML
  • XML Literals in Visual Basic

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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();

    More .NET Articles
    More By Peyton McCullough


       · Hello, all,This is a continuation of my...
     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek