Dapper Fundamentals in .NET Core: Simplify Your Database Access

Zuraiz Ahmed Shehzad
3 min readMar 27, 2023

--

What is Dapper ?

Dapper is a lightweight micro Object-Relational Mapping (ORM) tool for .NET applications. It allows developers to work with relational databases using simple SQL statements. Dapper provides a simple way to map database tables to .NET objects without the overhead of traditional ORMs.

Getting Started with Dapper

To use Dapper, you first need to install the Dapper NuGet package. You can install it by running the following command in the Package Manager Console:

Install-Package Dapper

Once you have installed the package, you can start using Dapper in your .NET Core application.

Connecting to a Database

To connect to a database using Dapper, you need to create a SqlConnection object and pass in the connection string. Here’s an example:

using System.Data.SqlClient;

var connectionString = "Server=myServerAddress;Database=myDataBase;
UserId=myUsername;Password=myPassword;";

using (var connection = new SqlConnection(connectionString))
{
//Code Here
}

Executing SQL Queries

Dapper provides extension methods for the SqlConnection object that allow you to execute SQL queries.

The most commonly used methods are:

  • Query
  • Execute

Query:

Query method is used to retrieve data from the database.

Here’s an example of using the Query method:

using System.Data.SqlClient;

var connectionString = "Server=myServerAddress;Database=myDataBase;
UserId=myUsername;Password=myPassword;";

using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var customers = connection.Query<Customer>("SELECT * FROM Customers");
}

In this example, we’re retrieving all the customers from the Customers table and mapping them to a list of Customer objects.

Execute:

Execute method is used to execute non-query SQL statements, such as inserts, updates, and deletes.

Here's an example of using the Execute method:

using System.Data.SqlClient;

var connectionString = "Server=myServerAddress;Database=myDataBase;
UserId=myUsername;Password=myPassword;";

using (var connection = new SqlConnection(connectionString))
{
connection.Open();
connection.Execute("INSERT INTO Customers (CustomerName, Age) VALUES (@Name, @Age)",
new { Name = "John Smith", Age = 30 });
}

In this example, we’re inserting a new customer into the Customers table.

Mapping Database Tables to Objects

Dapper uses a simple mapping convention to map database tables to .NET objects. By default, Dapper assumes that the property names of your .NET objects match the column names in your database table. If this is not the case, you can use the Column attribute to specify the column name.

Here's an example:

public class Customer
{
public int Id { get; set; }
[Column("CustomerName")]
public string Name { get; set; }
public int Age { get; set; }
}

In this example, we’re using the Column attribute to specify that the Name property should be mapped to the CustomerName column in the Customers table.

Conclusion

In this introductory blog post, we covered the basics of Dapper and its implementation in .NET Core. In the upcoming blogs, we will dive deeper into Dapper’s capabilities and explore its advanced features, such as mapping complex queries to objects, managing database transactions, and optimizing performance. Stay tuned for upcoming blogs, where we will take a practical approach to demonstrate how Dapper can simplify your database operations in .NET Core.

Before You Leave

  • Follow Me on Medium for tech savvy content. 😄
  • Connect with me on LinkedIn to talk about exciting world of software engineering. 🔗
  • Check out all of my coolest work on Medium. 👐

Are you following my series “Design Patterns In Action” ?

--

--

Zuraiz Ahmed Shehzad

Software engineer with a passion for cloud computing and system design. Committed to creating user-friendly solutions. Let's innovate together.