In this article we will learn how to get list of records from database using Dapper in ASP.NET MVC .If you wants to learn more about Dapper then please refer my following articles
Step 1 : Create Model Class
Step 2 : Create Stored Procedure to get records from database
- Installing Dapper ORM Into MVC Project
- CRUD Operations In ASP.NET MVC 5 Using Dapper
- Stored Procedure Parameter Mapping with Dapper
- How To Pass Generic List Using Dapper In MVC
- How To Pass Stored Procedure Using Dapper In MVC
Step 1 : Create 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; } }
Step 2 : Create Stored Procedure to get records from database
CREATE Procedure [dbo].[GetEmployees] as begin select Name,City,Address from Employee End
Step 3 : Now Create the function to bind the generic list with Dapper
public List<EmpModel> GetAllEmployees() { connection(); con.Open(); IList<EmpModel> EmpList = SqlMapper.Query<EmpModel>( con, "GetEmployees").ToList(); con.Close(); return EmpList.ToList(); }
- Connection() is the method which contains the connection string .
- Con is the SqlConnection class object.
- AddNewEmpDetails is the stored procedure.
- EmpModel is model class.
Summary
I hope this article is useful for all readers. If you have any suggestion then please contact me.
Download Aspose : API To Create and Convert Files..
Post a comment