Call Stored Procedure with Dapper

As you know day by day Dapper Micro ORM increasing popularity among developers because of its speed and less code. So in this article explains how to call stored procedure with Dapper.
Suppose you have following Model class

public class EmpModel
       [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; }

Now consider following stored procedure we have

Create procedure [dbo].[AddNewEmpDetails]  
   @Name varchar (50),  
   @City varchar (50),  
   @Address varchar (50)  
   Insert into Employee values(@Name,@City,@Address)  
Now following is the function which is used to pass above stored procedure to Dapper

public void AddEmployee(EmpModel objEmp)
            //Passing stored procedure using Dapper.NET
                con.Execute("AddNewEmpDetails", objEmp, commandType: CommandType.StoredProcedure);
In the above function

  •  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

Post a Comment

Protected by Copyscape
Copyright © Compilemode