In this article, we will learn how to use the SQL stored procedures with dapper ORM by writing a few lines of code. Let's consider we have the following values that need to be inserted into the SQL database using a 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) ) as begin Insert into Employee values(@Name,@City,@Address) End
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; } }
public void AddEmployee(Employee objEmp) { //Passing stored procedure using Dapper.NET connection(); con.Execute("AddNewEmpDetails", objEmp, commandType: CommandType.StoredProcedure); }
- 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