In this article we will learn how to map stored procedure parameter with Dapper ORM .To learn more about Dapper please refer my following articles
Suppose you have following Model class of parameters which values comes from UI
Now consider following stored procedure we have
Now following is the function which is used to map the stored procedure with Dapper
In the above function
Watch Video
Summary
I hope this article is useful for all readers. If you have any suggestion then please contact me.
Read my other MVC articles
- Installing Dapper ORM Into MVC Project
- CRUD Operations In ASP.NET MVC 5 Using Dapper
- How To Pass Generic List Using Dapper In MVC
- How To Pass Stored Procedure Using Dapper In MVC
Suppose you have following Model class of parameters which values comes from UI
public class EmpModel { [Display(Name = "Id")] public int Empid { get; set; } [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) ) as begin Insert into Employee values(@Name,@City,@Address) End
Now following is the function which is used to map the stored procedure with Dapper
public bool AddEmployee(EmpModel obj) { DynamicParameters param = new DynamicParameters(); param.Add("@Name", obj.Name);
param.Add("@City", obj.City);
param.Add("@Address",obj.Address);
connection(); con.Open(); con.Execute("AddNewEmpDetails", param, commandType: CommandType.StoredProcedure); con.Close(); return true; }
In the above function
- Connection() is the method which contains the connection string .
- Con is the SqlConnection class object.
- AddNewEmpDetails is the stored procedure.
- DynamicParameters is the Dapper class name to map parameter like as SqlCommand
- EmpModel is model class.
Watch Video
Summary
I hope this article is useful for all readers. If you have any suggestion then please contact me.
Read my other MVC articles
- ActionResult in ASP.NET MVC.
- Creating an ASP.NET MVC Application.
- CRUD Operations In ASP.NET MVC 5 Using ADO.NET .
- Convert DataTable To Generic List In ASP.NET MVC .
- How To Pass Stored Procedure Using Dapper.NET In MVC
- Show Confirm Alert Box on ActionLink Click In ASP.NET MVC.
- Convert DataTable To Generic List Using LINQ In ASP.NET MVC.
- How to Change ActionLink Text Color In ASP.NET MVC .
- How to Call Another Controller View Using ActionLink In ASP.NET MVC .
- Convert DataTable To Generic List Using AsEnumerable In ASP.NET MVC .
- How To Pass Generic List Using Dapper In MVC
- CRUD Operations In ASP.NET MVC 5 Using Dapper
Post a Comment