Browse DevX
Sign up for e-mail newsletters from DevX


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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

Dim dataContext As DataClasses1DataContext = _ New DataClasses1DataContext()

Then, use LINQ to select the required rows:

Dim allAuthors = From a In dataContext.authors _ Select a

Finally, print out the individual fields by accessing them directly via the various properties:

For Each a In dataContext.authors Console.WriteLine("{0} - {1}, {2}", _ a.au_id, a.au_fname, a.au_lname) Next

To retrieve all authors living in Utah and then bind the result to a DataGridView control, use the following statements:

Dim dataContext As DataClasses1DataContext = _ New DataClasses1DataContext() Dim allAuthors = From a In dataContext.authors _ Where a.state = "UT" _ Select a '---bind to a datagridview control--- DataGridView1.DataSource = allAuthors

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:

Dim library As XElement = _ <Library> <Book> <Title>Professional Windows Vista Gadgets Programming</Title> <Publisher>Wrox</Publisher> </Book> <Book> <Title>ASP.NET 2.0 - A Developer's Notebook </Title> <Publisher>O'Reilly</Publisher> </Book> <Book> <Title>.NET 2.0 Networking Projects</Title> <Publisher>Apress</Publisher> </Book> <Book> <Title>Windows XP Unwired</Title> <Publisher>O'Reilly</Publisher> </Book> </Library>

To save the xml document to file, use the Save() method:


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

The above code will generate the following output:

ASP.NET 2.0 - A Developer's Notebook Windows XP Unwired

Thanks for your registration, follow us on our social networks to keep up-to-date