Many times we need to insert list of records into the database instead of one by one , So in this article we will learn how to pass the generic list using Dapper.NET ORM in MVC .Please read my previous article to understand the basics about MVC:
Suppose you have following Model class
- 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 .
Suppose you have following Model class
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] ( @Id int=null), @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 pass above stored procedure to Dapper.NET
public void AddEmployee(List<EmpModel> EmpList) { //Passing generic List to stored procedure using Dapper.NET connection(); con.Execute("AddNewEmpDetails", EmpList, 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.
- EmpList is the reference of model class generic list
I hope this article is useful for all readers. If you have any suggestion then please contact me.
Post a Comment