Showing posts with label stored procedure in linq. Show all posts
Showing posts with label stored procedure in linq. Show all posts

Tuesday, 29 October 2013

LINQ to Stored Procedure(without parameter)

For Advance Asp.net Training Fill Enquiry Form
LINQ to Stored Procedure
(Without Parameter)

LINQ also provide facility to connect with Stored Procedure. Following is the table which I am using to explain this topic.

Table Name:-Emp


Figure 1

To work with LINQ to SQL we have to add LINQ Classes. Using Add New Item add LINQ to SQL Classes.



Figure 2

Now create class to map with Table Emp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping;

[Table(Name="Emp")]
public class Class1
{
    //IsDbGenerated represents that it is Identity column
    [Column(IsPrimaryKey=true, IsDbGenerated=true)]
    public int id;
    [Column]
    public string name;
    [Column]
    public int age;
}

Follow the following steps:-

Step 1:-

Create Stored Procedure

create  proc selectAllData
as
select * from emp

Step 2:-

To work with stored procedure in LINQ we have to use DataContext class. To run stored procedure we have to use ExecuteMethodCall which is protected type. So we have to first inherit the DataContext Class to call the method ExecuteMethodCall ().