Locations of visitors to this page


Linq to DataSet

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009

Linq to DataSet

Linq allows you to query the data in a dataset.  For this example I load the Products and Categories table from the Northwind Database.  I then do a join query to export the Product Name, Unit Price, and Category Name into a list which I display in a datagridview.  Note a query of this type will not display in a datagridview you need to set the datasource equal to the query's Tolist method.

Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strConn As String = _
                "Server = .\SQLEXPRESS;Database = NorthWind; Integrated Security = SSPI;"
        Dim conn As New SqlConnection(strConn)
        Dim da As New SqlDataAdapter("Select * from Products; Select * from Categories", conn)
        Dim ds As New DataSet
        da.Fill(ds)
        Debug.Print(ds.Tables(1).TableName)
        Dim products As DataTable = ds.Tables(0)
        Dim categories As DataTable = ds.Tables(1)
        Dim query = From p In products.AsEnumerable _
                    Join c In categories.AsEnumerable _
                    On p.Field(Of Int32)("CategoryID") Equals c.Field(Of Int32)("CategoryID") _
                    Select New With {.ProductName = p.Field(Of String)("ProductName"), _
                                    .Price = p.Field(Of Decimal)("UnitPrice"), _
                                    .Category = c.Field(Of String)("CategoryName")}

        DataGridView1.DataSource = query.ToList
    End Sub
End Class

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Linq | VB | VS 2008
Posted by Ken Tucker on Thursday, November 08, 2007 11:05 AM
Permalink | Comments (1) | Post RSSRSS comment feed

Related posts

Comments

busby seo test us

Sunday, December 21, 2008 4:20 PM

busby seo test

nice post!thanks for the info.just keep it up

Comments are closed