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
Now let's consider the scenario. We have a stored procedure and a model class from which we want to get a list of records from a database with the help of Dapper ORM.
Now let's consider the scenario. We have a stored procedure and a model class from which we want to get a list of records from a database with the help of Dapper ORM.
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 suggestions, then please contact me.
Post a Comment