Dapper Using SQL Stored Procedure

SQL stored procedures can be used with dapper ORM by writing the few lines of code. Let's consider we have following values needs to be inserted into the SQL database using stored procedure with the help of dapper ORM

  • Name
  • City
  • Address

Create the stored procedure to map the above parameters

Create procedure [dbo].[AddNewEmpDetails]  
   @Name varchar (50),  
   @City varchar (50),  
   @Address varchar (50)  
   Insert into Employee values(@Name,@City,@Address)  

Create the C# property class to map the above stored procedure parameters

    public class Employee

       [Required(ErrorMessage = "First name is required.")]
        public string Name { get; set; }
        [Required(ErrorMessage = "City is required.")]
        public string City { get; set; }
        [Required(ErrorMessage = "Address is required.")]
        public string Address { get; set; }


Create the method to insert the values using the dapper

       public void AddEmployee(Employee objEmp)

            //Passing stored procedure using Dapper.NET
                con.Execute("AddNewEmpDetails", objEmp, commandType: CommandType.StoredProcedure);

In the above method
  • Connection() is the method which contains the connection string .
  • Con is the SqlConnection class object.
  • AddNewEmpDetails is the stored procedure.
  • ObjEmp is the object of model class

I hope this article is useful for all readers. If you have any questions then type in comment box.


Easy and simple. Thanks for the example, very helpful


Post a Comment


Protected by Copyscape
Copyright © Compilemode