Get List of Records From DataBase Using Dapper In ASP.NET MVC

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 lets consider the scenario  ,We have stored procedure and model class from which we wants to get list of records from 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();
         
        }

In the above function
  •  Connection() is the method which contains the connection string .
  •  Con is the SqlConnection class object.
  • AddNewEmpDetails is the stored procedure.
  • EmpModel is  model class.
From preceding examples we learned how to get list of records from database with the help of Dapper ORM.

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

www.CodeNirvana.in

Protected by Copyscape Online Copyright Protection
Copyright © Compile Mode