CRUD Using Dapper In C#

 Introduction:

In this article, we will learn that how can we implement the Dapper ORM, and we will see how can we implement the crud operations using dapper.

So dapper is an ORM, which stands for object relational mapping, it is a library we can use for connecting with our backend database, 

We have other ORM libraries as well such as entity framework etc., which we will see in our later articles,

As shown in the above image, that we first create a class in our main application, and then by using an ORM like Dapper or Entity Framework, we connect to the database at the backend, and then by creating a table in the database we map the table columns to the class properties in the application software.

So here, in this example, we are going to perform the crud operations in Dapper and will implement the following operations.

  1. Execute
  2. Query
  3. QueryFirstOrDefault
  4. QuerySingle
  5. QuerySingleOrDefault
  6. QueryMultiple

Let’s start with the implementation of CRUD operation using dapper.

Step 1:  

So, first up create a new console application project in Visual Studio 2017, and we will name it as DapperExample and then click OK.



Step 2:

Adding the latest nuget package for Dapper by right clicking the console application project and choose manage nuget packages.

Note: you would have to choose the correct nuget version for dapper which is compatible with the .net framework you are using. 


Step 3:

So by following the three tier architecture, we will write a method for fetching the data inside the data access layer using dapper.


as in the image above, Line 59 indicates that Query method takes the SQL query to perform the data retrieval operation using the modal student.

 Step 4:

Similarly, we will add methods for insert update and delete inside the data access layer. 


and this is how the methods inside the Business Access Layer looks like,


and Finally, we can make calls to all those methods inside business layers from our presentation layer, which is the program.cs of out main console application called DapperExample.

The above code snippets indicates the execution of all the crud operation in the main function of the program.cs of the console application DapperExample,

First it retrieves the list of students by calling the method inside the business layer followed by performing the insert, update and delete operations by calling the respective methods inside the business access layer.

The final output after execution of the code is this,








Talha Mahmood

I have been developing Applications in Microsoft .NET technologies since 2015, and having more then 5 years of experience in .NET Technology and SQL database, also having hands in tools like C# Windows Forms, WPF, WCF, Asp.Net Web forms, Asp.Net Core, SSRS, LINQ, Entity Framework, Crystal Reports, SQL server, MySQL, MongoDB, Document DB, JQuery, JavaScript, Oracle, Web API.

Post a Comment

Previous Post Next Post