In LINQ to SQL, we can use stored procedures with or without parameters to get the required data from database tables.
Before we start using LINQ to SQL with a stored procedure, we need to create a database with required tables and map those tables to LINQ to SQL file (.dbml). If you don't know the process check this link create and map database tables to LINQ to SQL file (.dbml).
Once our database is ready with the required tables, we will create a simple stored procedure to get employee details from the EmployeeDetail table.
Write stored procedures like as shown below and execute them in your database.
Once we execute the above stored procedure in the database, that will be like as shown below.
Now drag and drop that stored procedure on the right-hand pane of the LINQ to SQL dbml class, as shown below.
Once we create and map required tables and stored procedures to the .dbml file now, we will show data in our application for that Right click on the application à select Add à New Item à Select Web Form à Give name as Default.aspx and click OK button.
Now open the Default.aspx page and write the code as shown below.
Now open the code behind file and write the code as shown below.
C# Code
VB.NET Code
If you observe the above example, we are using stored procedure “GetEmployeeDetails” with parameters in LINQ to SQL to get data from the database.
Following is the result of LINQ to SQL with a stored procedure to get data from the database.
This is how we can use LINQ to SQL with stored procedures to get the required data from the database in c#, vb.net.