This article has three parts:
- Introduction to Stored Procedure and LINQ To SQL.
- Simply uses of Stored Procedure with LINQ.
- CRUD operations with uses of Stored Procedure with LINQ To SQL
Introduction to Stored Procedure and LINQ To SQL:
Stored Procedure: Collection and Group of T-SQL commands is called Stored procedure. In this we can write commands of Creation, updation and deletion of parts of Data Definition Language (DDL), Data Manipulation Language (DML).
We all prefer stored procedure writing compared to writing hardcoded query. There are so many reasons to prefer Stored Procedures.
Stored Procedure can do: Input Parameter, Output Parameter and Output the bunch of records.
Why We Should Use Stored Procedure?
- Compile one time only.
- Executing fast as compare to hardcoded query.
- User level protection.
- Reduce network traffic.
LINQ to SQL:
Language Integrated Query (LINQ), Linq to SQL works as ORD (Object Relational Designer). Before starting to work on LINQ To SQL you should understood the ORD.
ORD is a canvas where you can create or drag and drop the table or entity on it from Server Explorer. Linq to sql designer can be created with Add New Item and Select LINQ to SQL classes template and you can see that creating a DBML extension kind of file.
Thats DBML is called Object Relational Designer (O/R Designer).
This DBML is used to create entity classes that map to database tables or views and other important things.
Naming Convention of tables you should take care.. Singular or Pluralisation.
There two way to change table or entity name in DBML.
- You can change either from database itself.
- Tools - Options Database Tools and select O/R Designer then select False from the Enabled Drop Down list in the Pluralisation of Names group.
Simple Uses of Stored Procedure with LINQ To SQL:
Herewith we are going to use Stored Procedure with Linq To Sql step by step.
Stored Procedure returning the list of friends.
stpGetAllFriends code
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- -- =============================================
- -- Author: Manoj kalla
- -- Create date: 28-March-2016
- -- Description: To Get All Friends.
- -- =============================================
- CREATE PROCEDURE stpGetAllFriends
- AS
- BEGIN
- -- SET NOCOUNT ON added to prevent extra result sets from
- -- interfering with SELECT statements.
- SET NOCOUNT ON;
- Select * From tblFriends
- END
- GO
- Database name is MemberCDAC its having tblFriends and stpGetAllFriends,

- Create a new Asp.Net Web Site project named : StoreProceduredWithLinqToSql,

- Right click on solution explorer and select Add New Item,
or press CTRL + SHIFT + A,
Select LINQ to SQL Classes item
Named item as FriendDataClasses.dbml,
Press Yes on above dialogue box. - Select VIEW option from Visual Studio and select SERVER EXPLORER option or press simply CTRL + W + L,

You can see and compare previous screen shots of SQL Server with SERVER EXPLORER its same with contents. - Drag N Drop stpGetAllFriends object which inside under section of Stored Procedures.

After draging and dropping our DBML will look like like the above image. - Now right click on solution explorer and add new item and add WEB FORM named it : GetAllFriends.aspx

- From toolbox select Data group/section under there is a GRIDVIEW control.
Select GridView’s smart tag option select AUTO FORMAT,
- GetAllFriends.aspx code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetAllFriends.aspx.cs" Inherits="GetAllFriends" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
- <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
- <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
- <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
- <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
- <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
- <SortedAscendingCellStyle BackColor="#FFF1D4" />
- <SortedAscendingHeaderStyle BackColor="#B95C30" />
- <SortedDescendingCellStyle BackColor="#F1E5CE" />
- <SortedDescendingHeaderStyle BackColor="#93451F" />
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
- GetAllFriends.aspx.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class GetAllFriends: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- FriendDataClassesDataContext _db = new FriendDataClassesDataContext();
- GridView1.DataSource = _db.stpGetAllFriends();
- GridView1.DataBind();
- }
- }
- Output

CRUD operations with uses of Stored Procedure with LINQ To SQL
Now in this section we are implementing CRUD (Create, Retrieve, Update, Delete) operations against table of database with uses of Stored Procedure running with the help of LINQ TO SQL.








Ramzanali MominPosted Sep 10, 2018, 6:53 AM
Very nice for beginner
Rahul Kumar SaxenaPosted Apr 4, 2016, 7:43 AM
Good one
Humayun Kabir MamunPosted Apr 4, 2016, 6:17 AM
Nice...
Vignesh ManiPosted Mar 31, 2016, 8:55 AM
Good one
Rahul SharmaPosted Mar 31, 2016, 5:24 AM
any article or book on starting with stored procedure
Jaipal ReddyPosted Mar 31, 2016, 12:06 AM
nice one sir. .
Roshan MulePosted Mar 30, 2016, 2:07 PM
Good Article Sir. Very Useful.