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

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

Rate This
  • Comments 6

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
Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post
Page 1 of 1 (6 items)