Introduction
Sometimes we need to create APIs and perform custom operation with database where we can perform create, insert, update and delete on entity/ties or table/s according to our requirement.
Then we can use Store Procedure.
DbContext will provide us Connection String and Connection String will provide Database Connection.
And using SqlCommand we can execute the Stored Procedure which will give us SqlDataReader in which we will get result/response/output of Stored Procedure.
Using DbContext we can get Database Connection and then Connection String to perform needful operations using stored procedure.
We will see the complete tutorial with steps to know how to use Stored Procedure in respository by using DbContext.
Here, we will see single READ/RETRIEVE/GET operation using stored procedure.
By following the below steps you will come to know how to use stored procedure.
1. Create Table
Consider the Entity/Table fields as follows
Need to create table.
CREATE TABLE [dbo].[Students](
[RollNo] [int] IDENTITY(1,1) NOT NULL,
[CreatedBy] [nvarchar](max) NULL,
[CreatedDate] [datetime2](7) NOT NULL,
[LastModifiedBy] [nvarchar](max) NULL,
[LastModifiedDate] [datetime2](7) NULL,
[FirstName] [nvarchar](max) NULL,
[MiddleName] [nvarchar](max) NULL,
[LastName] [nvarchar](max) NULL,
[First_LastName] [nvarchar](max) NULL,
[FullName] [nvarchar](max) NULL,
[Address] [nvarchar](max) NULL,
[Mobile] [nvarchar](max) NULL,
[Email] [nvarchar](max) NULL,
[Standard] [nvarchar](max) NULL,
[MediumType] [int] NOT NULL,
CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED
(
[RollNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
2. Stored Procedure
We can write Stored Procedure as per the requirement.
a. Stored Procedure Code
CREATE PROCEDURE [dbo].[GetAllStudentsByMediumType]
@mediumType int=0
AS
BEGIN
SELECT *
FROM Students student
WHERE student.MediumType = @mediumType
END
b. Stored Procedure Execution
Command used to execute the stored procedure.
EXEC [dbo].[GetAllStudentsByMediumType] @mediumType=1;

3. Contract | Interface
This is method declaration inside contract/interface which will get implemented in the interface IStudentRepository.
using ILogicWebAPI.Domain.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ILogicWebAPI.Application.Contracts.Persistence
{
public interface IStudentRepository : IAsyncRepository<Student>
{
Task<List<Student>> GetStudentsByMediumTypeAsync(int MediumType);
}
}
4. Repository
StudentRepository where we will implement the method declared inside contract/interface
using ILogicWebAPI.Application.Contracts.Persistence;
using ILogicWebAPI.Domain.Entities;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace ILogicWebAPI.Persistence.Repositories
{
public class StudentRepository : BaseRepository<Student>, IStudentRepository
{
private readonly ILogger _logger;
public StudentRepository(ILogicApplicationDbContext dbContext, ILogger<Student> logger) : base(dbContext, logger)
{
_logger = logger;
}
public async Task<List<Student>> GetStudentsByMediumTypeAsync(int MediumType)
{
List<Student> lstStudents = new List<Student>();
var conString = _dbContext.Database.GetDbConnection();
using (SqlConnection con = new SqlConnection(conString.ConnectionString))
{
SqlCommand cmd = new SqlCommand("GetAllStudentsByMediumType", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@mediumType", MediumType);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Student student = new Student();
student.RollNo = Convert.ToInt32(rdr["RollNo"]);
student.FirstName = rdr["FirstName"].ToString();
student.MiddleName = rdr["MiddleName"].ToString();
student.LastName = rdr["LastName"].ToString();
student.First_LastName = rdr["First_LastName"].ToString();
student.FullName = rdr["FullName"].ToString();
student.Address = rdr["Address"].ToString();
student.Mobile = rdr["Mobile"].ToString();
student.Email = rdr["Email"].ToString();
student.Standard = rdr["Standard"].ToString();
student.MediumType = Convert.ToInt32(rdr["MediumType"]);
student.CreatedBy = rdr["CreatedBy"].ToString();
student.CreatedDate = Convert.ToDateTime(rdr["CreatedDate"].ToString());
student.LastModifiedBy = rdr["LastModifiedBy"].ToString();
student.LastModifiedDate = Convert.ToDateTime(rdr["LastModifiedDate"].ToString());
lstStudents.Add(student);
}
con.Close();
}
return lstStudents;
}
}
}

Join the conversation! Your thoughts help the community grow.