3 min read

Introduction

LINQ, short for Language Integrated Query, provides an object oriented approach to not only querying relational databases but also any kind of source such as XML, Collection of objects, etc. LINQ to SQL provides (O/R) object-relational mapping and Visual Studio 2008 IDE provides a (O/R) designer. Visual Studio 2008 also has a web server control called LinqDataSource control. This control requires a DataContext which is provided by the LINQ-to-SQL classes, a class generator that maps SQL objects to the model. Without this control one may have to generate the classes from scratch or by using the SQLMetal.exe utility which generates tables and columns. The readers may benefit by reading my  previous article on Displaying SQL Server Data using a Linq data source. We will continue to use the PrincetonTemp table used in the previous article on MySQL Data Transfer using Sql Server Integration Services. We will map this table to LINQ via LinqDataSource Control and then use it as the source of data for the chart.

Create a Framework 3.5 Web Site project

Open Visual Studio 2008 from its shortcut on the desktop. Click File | New | Web Site…(or Shift+Alt+N) to open the New Web Site window. Change the default name of the site to a name of your choice (herein PieChartWithLinq) on your local web server as shown. Make sure you are creating a .NET Framework 3.5 web site as shown here. This is very similar to creating a web site that can reside on the file system.

WCF Multi-tier Services Development with LINQ

Add a LinqDataControl and provide the data context

Drag and drop a LinqDataSource control from Toolbox | Data shown in the next figure on to the Default.aspx

WCF Multi-tier Services Development with LINQ

This creates an instance of the control LinqDataSource1 as shown.

WCF Multi-tier Services Development with LINQ

The source page Default.aspx will display the  control as shown in the next figure.

WCF Multi-tier Services Development with LINQ

When you try to configure a data source for this control using the smart tasks (see Figure 3) the program would take you to a window which does not allow you create a new data context. But if there are existing items the program allows you to choose.

Create a data context for the LinqDataSource control

We will add a new data context. Right click the web site in Solution Explorer and pick Add New Item… from the drop-down menu. This opens the Add New Item window as shown. Highlight the item Linq to SQL Classes in the Add New Item window. Replace the default name DataClasses.dbml with one of your own. Herein it is replaced with Princeton.dbml.

WCF Multi-tier Services Development with LINQ

When you click OK after renaming you will get a Microsoft Visual Studio warning as shown. Read the message on this window.

WCF Multi-tier Services Development with LINQ

Click Yes to add Princeton.dbml consisting of two items to the App_Code folder on the web site project as shown. One is a layout designer and the other a code page.

WCF Multi-tier Services Development with LINQ

The (O/R) designer containing two panes is shown in the next figure. In the left pane you drag and drop table (s) from the server explorer assuming you a made a connection with a database. To the right you can add a stored procedure from the server explorer. Read the instructions on this page.

WCF Multi-tier Services Development with LINQ

Click (View | Server Explorer) to display the connections in the server explorer as shown. In the next figure you can see the expanded node of TestNorthwind displaying the table PrincetonTemp in the SQL Server 2008 Hodentek2Mysorian. The database and the server names are the ones used for this article and you may have different names in your machine. The connection used is already present but you can create a new connection in the server explorer window if you choose to do so.

WCF Multi-tier Services Development with LINQ

Drag and drop the PrincetonTemp table to the left pane of the (O/R) designer as shown. Build the project. This will make the objects available for the LinqDataSource1 on the default page.

WCF Multi-tier Services Development with LINQ

LEAVE A REPLY

Please enter your comment!
Please enter your name here