What's New in Visual Basic 9.0? Part 2 : Page 3

The release of Visual Studio 2008 updates Visual Basic to version 9.0, with several key language enhancements and LINQ support. Part Two of this series will walk you through VB 9's new LINQ support features and other time-saving enhancements.

by Wei-Meng Lee

Mar 18, 2008

Page 3 of 4

WEBINAR:

On-Demand

Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

LINQ to SQL
The previous section showed you how to use LINQ to retrieve data from a dataset. A much easier way is to use LINQ to SQL, which allows you to access SQL databases directly.

First, add a new item to your project. Select the Data category and then the LINQ to SQL Classes template. Use DataClasses1.dbml as your default name.

In Server Explorer, establish a connection to the desired database and drag and drop the desired table onto the DataClasses1.dbml page.

Visual Studio 2008 creates a new class called DataClasses1DataContext (which derives from DataContext). This class allows you to programmatically connect to (and access) the authors table. This class is found in the DataClasses1.designer.vb file.

To use LINQ to retrieve the rows in the authors table, first create an instance of the DataClasses1DataContext class:

LINQ to XML
Another very cool capability of LINQ is its ability to manipulate XML documents. For example, if you want to create an XML document by hand, you can use the following code snippet in Visual Studio 2008:

To perform querying on the XML document, load the document from file and then use LINQ to XML:

Dim LibraryBooks As New XDocument
LibraryBooks = XDocument.Load("Books.xml")
Dim OReillyBooks As IEnumerable = _
From b In LibraryBooks.Descendants("Book") _
Where b.Element("Publisher") = "O'Reilly" _
Select b.Element("Title").Value
For Each book In OReillyBooks
Console.WriteLine(book)
Next