Linq Cookbook, Recipe 6: Your first Linq Application using Northwind (Kit George)

Published 11 July 07 07:48 PM

This example is intended as a ‘primer’ for following recipes (so we don’t repeat the same instructions multiple times). This recipe takes you through everything you need, and assumes only that you've installed Visual Studio (and in so doing, you've installed SQLExpress). Subsequent Northwind based Linq-To-Sql recipes will point to this entry.

Ingredients:

-          Visual Studio 2008 (Beta2 or Higher)

            We assume that SQL Express (or better) has been installed

 

Categories: LINQ-To-SQL, LINQ and WinForms

 

Instructions:

  • We need the northwind database, so Download the northwind database here. Run the file from the download button on the page
  • Once downloaded, you’ll find the new samples in “C:\SQL Server 2000 Sample Databases”. We’ll be using the northwind.mdf file
  • Open Visual Studio, and create a new Windows Forms Application
  • Go to the View menu and click the Server Explorer menu item
  • On the treeview on the left, right-click the Data Connections and select ‘Add Connection’
  • In the next dialog select ‘Microsoft SQL Server Database File’ and click Continue
  • In the next dialog, click the Browse button, and navigate to “C:\SQL Server 2000 Sample Databases”. Select the ‘NORTHWND.MDF’ file, and click Open
  • Click OK to finish Adding the Connection
  • In your Treeview node, under ‘Data Connections’ NORTHWIND.MDF will be available. Expand this node (click on the plus beside it) and expand the Tables node
    • Note: The steps up to this point need only be completed once. If you create a new project, then the database will still be available through server explorer. You may need to reopoen the Server Explorer window from session to session (see below)
  • Now add a Linq To Sql file to your application. Go to the Project menu, and select ‘Add New Item’
  • In the Add New Item dialog, select the ‘LINQ To SQL Classes’ item
  • Change the name to ‘Northwind.dbml’ and click Add
  • From the Server Explorer window (which will still be open) select all of the Tables from the node we expanded (DataConnections\NORTHWND.MDF\Tables) and drag them to the O/R designer window to the right (this window will have opened after we added the Northwind.dbml file)
  • You will get a dialog asking if you wish to copy the database locally. Click Yes
  • Close the designer file, and the Server Explorer window. You’re ready to start writing your LinQ To Sql code!
  • To reopen the Server Explorer window, go to the View menu, and select Server Explorer
  • To reopen the O/R designer, go to the Solution Explorer window, and double-click Northwind.dbml

 Test our project

  • To ensure our project is ready to add linq statements to, let’s try it out!
  • Add a button to form1
  • Add a DataGridView to form1
  • Double-Click the button to open its click event
  • Add this code inside the click event:

Dim db As New NorthwindDataContext

DataGridView1.DataSource = _

     From cust In db.Customers _

     Where cust.City = "London" _

     Order By cust.CompanyName _

     Select cust.CompanyName, _

     cust.ContactName, cust.Fax

  • Run the App (F5) and click the button to test it

Comments

# University Update - Microsoft SQL Server - Linq Cookbook, Recipe 6: Your first Linq Application using Northwind said on July 12, 2007 12:51 AM:

PingBack from http://www.universityupdate.com/Technology/Microsoft_SQL_Server/3781823.aspx

# Thiago Cruz [MVP] said on July 12, 2007 12:21 PM:

Do you have a Interop Tool Kit with VB9?

- Migration VB6 for VB9...

# VBTeam said on July 12, 2007 1:55 PM:

Hi Thiago,

Yes, the Interop Forms Toolkit 2.0 works with Visual Studio 2008 as well.

Hope that helps,

Jonathan Aneja

Program Manager

VB Team

# The Visual Basic Team said on July 12, 2007 2:14 PM:

Ingredients: - Visual Studio 2008 (Beta2 or Higher) - Connection to Northwind database Categories: LINQ-To-Objects,

# Noticias externas said on July 12, 2007 2:53 PM:

Ingredients: - Visual Studio 2008 (Beta2 or Higher) - Connection to Northwind database Categories: LINQ

# The Visual Basic Team said on November 7, 2007 4:13 PM:

Ingredients: · Visual Studio 2008 (Beta2 or Higher) Categories: LINQ to SQL Introduction: As we add cookbook

New Comments to this post are disabled

This Blog

Syndication

Page view tracker