Skip Navigation LinksHome > Articles > LINQ > LINQ To SQL - I

LINQ To SQL - I

Creating Datacontext Using LINQ

By Keyur   On   Monday, 18 August 2008

Page Views : 2394   |   Technologies : LINQ

Rating : Rated :
0

In last session we saw some of the features we can use of LINQ to SQL while programming with SQL Server. From this article till end of its series I will explain you a step by step process to use LINQ TO SQL feature in your application. Throughout this we will work on a single application called FavouriteManager. This application is very simple where user can manage their favorite links, they can also do categorization of their links and create their custom categories, edit them and delete them as well. Three main tables we will use in this application are:

 

a)      lnkUsers

b)      lnkCategories

c)       lnkLinks

 

Don’t worry if you don’t understand anything as I am going to attach the entire source code along with the script of the database creation as well. So let’s begin with this application. First of all create a web application and if you are using 2005 please take the AJAX Enabled ASP .NET Website because we might use some of these features in our application. The first thing is to do is add a new file into your application.

 

At this stage I assume you already have created the database and also added relationships to them in your database. If you haven’t done so I kindly request you to get this done before you move further on this tutorial. If you are in hurry and want a ready to use database script please use the one that I have provided in my project download file.  Ok now you have database ready to use. Now got to your project and go to add item, you should see screen similar to this.

Now from this please select LINQ to SQL Classes option give it a proper name and then hit Add. It will ask you that it can be added to App_Code folder, go ahead and say yes. Now your new dbml file is created inside App_Code folder. Now the next step is to add the tables on this dbml file. Open your server explorer in Visual Studio and drill down to your database as below and add these tables onto your dbml file.

Once that id done successfully you should see something similar to the diagram below.

At first look it will look something like a class diagram you have created and that’s true its nothing but the class representation of your underlying data structure. If you see it will take care of the table’s relationships you created inside your SQL server while creating database. Now at this stage our major part is done. Now what is remaining is how you want to access your data. If you want to access data using Stored Procedure you can map your SP to this file as well. We will discuss that in a while but let’s see what we have created so far. Go to your default.aspx.cs page and write code below.

 

protected void Page_Load(object sender, EventArgs e)

    {

        LinkManagerDataContext lnk = new LinkManagerDataContext();

        var links = from lk in

                        lnk.lnkLinks

                    where lk.UserId == 1

                    select new

                    {

                        URL = lk.ActualUrl,

                        Comment = lk.UrlComments

                    };

        grdLinks.DataSource = links;

        grdLinks.DataBind();

    }

 

ASPX Page:

 

 <asp:GridView ID="grdLinks" runat="server" BackColor="White" BorderColor="#CCCCCC"

            BorderStyle="None" BorderWidth="1px" CellPadding="3">

            <RowStyle ForeColor="#000066" />

            <FooterStyle BackColor="White" ForeColor="#000066" />

            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />

            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />

        </asp:GridView>

 

And now run your application. You should see following result. Of course you need to put something in your database first. I have already added few rows for demonstration purpose.

 


Keywords :
Tags :
Rate This Article :

Comments :
Write a Comment / Question / Feedback ...


User Login
Username :
Password :
Register Login

Forgot Password


Related Articles