LINQ to
SQL - call stored procedure without parameters
Before
writing a LINQ to SQL query, we first need to create a DataContext using
the LINQ to SQL Classes template, to know more on how to create the
DataContext refer to the post LINQ to SQL Sample
Once
the DataContext is in place we can go ahead and use it to process data from the
mapped database.
Assume that we have the following StoredProcedure [GetEmployees], in the database
ALTER PROCEDURE
[dbo].[GetEmployees]
AS
BEGIN
SET NOCOUNT ON;
SELECT E.ID, E.Name,E.Phone,E.Email, D.Name as Department
From Employee E INNER
JOIN DEPARTMENT D
On E.DepartmentID = D.ID
END
Before using LINQ-to-SQL to
call the StoredProcedure we need to add the StoredProcedure to the right side
panel of the DataContext (.dbml) file.
Open the server Explorer
and drag the procedure to the right side panel of the .dbml file, your panel
should look as follows.
Once this is done we are
ready to call the StoredProcedure from the code, the code is as follows.
EmployeeClassesDataContext dbContext = new EmployeeClassesDataContext();
var EmployeeDetails =
dbContext.GetEmployees();
grdEmployees.DataSource
= EmployeeDetails;
grdEmployees.DataBind();
That’s it, the result of
the StoredProcedure will get bound to the grdEmployee GridView
No comments:
Post a Comment