Database Providers With Dapper

Dapper supports a variety of database providers, including the popular Microsoft SQL Server, Oracle, MySQL, MariaDB, PostgreSQL, SQLite, etc.

  • It is important to note that Dapper does not provide any database abstraction layer like Entity Framework Core or NHibernate.
  • Instead, it provides the user with several methods and extension points to extend the library for their custom database provider.
  • This is possible because it uses the simple ADO.NET data providers that come with the .NET framework, so it can easily work with any existing database technology that supports ADO.NET.
  • Dapper provides asynchronous support for all databases supported in ADO.NET.
  • It also provides a range of powerful features, such as parameterized queries and native commands for all major database engines.
🛠️ Customize Your Bulk Operations

Need custom keys, identity handling, or specific column mappings? Dapper Plus gives you powerful options to control how your data is inserted, updated, and merged.

Explore Dapper Plus Features →

Dapper SQL Server

Using Dapper to query SQL Server is straightforward. The first step is to install Microsoft.Data.SqlClient NuGet package. You can also do it through the NuGet Package Manager Console by typing the following command.

language-csharp
|
PM> NuGet\Install-Package Microsoft.Data.SqlClient

Using Dapper to access SQL Server is relatively easy. You first create a connection and start using the available methods from Dapper.

language-csharp
|
// using Dapper;

var connectionString = "Server=localhost;Initial Catalog=DapperDB;Integrated Security=true;TrustServerCertificate=True";

// Connect to the database 
var connection = new SqlConnection(connectionString);

// Query data with Dapper
var sql = "SELECT * FROM Product WHERE CategoryID = @categoryID";
var products = connection.Query<Product>(sql, new { categoryID = 1}).ToList();

Online Example

Once you have created the connection, you can use any Dapper Query methods to map the data returned from your query into a strongly typed object (or any other supported querying methods). In addition, it includes executing stored procedures to retrieve data from the database.

Dapper Oracle

To start using Oracle with Dapper, you must install the Oracle.ManagedDataAccess.Core NuGet package.

Once the connection is created, you can begin writing code that uses Dapper's built-in methods for interacting with the database. These include Execute, Execute Reader, etc.

language-csharp
|
// using Dapper;

var connectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1522)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl19 )));User Id=system;Password=z;";

// Connect to the database 
using (var connection = new OracleConnection(connectionString)) 
{    
    // Create a query that retrieves all books with an author name of "John Smith"    
    var sql = "SELECT * FROM Books WHERE Author=:authorName";     

    // Use the Query method to execute the query and return a list of objects    
    var books = connection.Query<Book>(sql, new {authorName="John Smith"}).ToList(); 
}

Dapper SQLite

Dapper offers an easy way to access and manipulate data in an SQLite database. With its simple API and powerful features, Dapper can significantly reduce the amount of code needed to perform these tasks. As a result, it is an excellent choice for working with small datasets or prototyping applications.

To start using SQLite with Dapper, you must install the System.Data.SQLite.Core NuGet package.

To execute any SQL statement, such as returning the first author found by using QueryFirst or QuerySingle if only a single row is returned.

language-csharp
|
// using Dapper;

var connectionString = "Data Source=D:\\DapperDB.db;";

// Connect to the database
using (var connection = new SQLiteConnection(connectionString))
{
    // Create a query that retrieves all authors    
    var sql = "SELECT * FROM Authors LIMIT 1;";     

    // Use the Query method to execute the query and return the first author
    var author = connection.QueryFirst<Author>(sql);
}

Dapper MySQL

When working with MySQL in .NET Core, you must install the following NuGet package MySql.Data.

Using Dapper to retrieve data from MySQL is simple and can be done in a few lines of code. First, create a connection, then use any query method provided by Dapper, such as ExecuteScalar.

language-csharp
|
// using Dapper;

var connectionString = "Server=localhost;database=DapperDB;Uid=root;Pwd=;Charset=utf8;Port=3307;SslMode=none";

// Connect to the database
using (var connection = new MySqlConnection(connectionString))
{
    // Create a query that retrieves all authors   
    var sql = "SELECT * FROM Authors";     

    // Use the Query method to execute the query and return a list of objects
    List<Author> authors = connection.Query<Author>(sql).ToList();
}

Dapper PostgreSQL

Dapper enables developers to easily and quickly query their PostgreSQL databases without writing complex SQL statements.

  • With Dapper, developers can easily map data from Postgres tables into .NET objects in minutes.
  • This makes retrieving, storing, and manipulating data in PostgreSQL incredibly simple.

To start using PostgreSQL with Dapper, you must install the Npgsql NuGet package.

language-csharp
|
// using Dapper;

var connectionString = "Server=127.0.0.1;Port=5432;Database=DapperDB;User Id=postgres;Password=z;";

// Connect to the database
using (var connection = new NpgsqlConnection(connectionString))
{
    // Create a query that retrieves all authors   
    var sql = "SELECT * FROM Authors";     

    // Use the Query method to execute the query and return a list of objects
    List<Author> authors = connection.Query<Author>(sql).ToList();
}

Like other providers, you must first create a connection and use any available Dapper methods, including Dapper Bulk Operations methods.

🔄 Synchronize Your Database in One Step

Keep your database synchronized with your incoming data. BulkSynchronize inserts new records, updates existing ones, and deletes missing ones.

Explore BulkSynchronize →

FAQ

What database providers are supported in Dapper?

Dapper supports a wide range of database providers that are compatible with ADO.NET, including but not limited to Microsoft SQL Server, MySQL, MariaDB, PostgreSQL, Oracle, and SQLite.

Since Dapper has no database-specific implementation, it works with pretty much all providers.


How do I install a database provider in Dapper?

To install a database provider in Dapper, you need to add the appropriate NuGet package to your project. For example:

More than one package might exist for the same providers. We have just included the most popular one.


Is ADO.NET provider supported in Dapper?

Yes, Dapper is built on top of ADO.NET and supports all databases that have an ADO.NET provider. This allows Dapper to work with a wide range of database technologies, including SQL Server, MySQL, MariaDB, PostgreSQL, Oracle, SQLite, and more.


Are there any performance considerations when using a specific database provider in Dapper?

Dapper is designed to be very efficient, but actual performance can depend on several factors, such as the specific database provider, the complexity and efficiency of your queries, the size of the result set, and the load on the database server. Always ensure your SQL queries are optimized for best performance.



Date Modified: 2025-07-09
Author:

Edit this page in GitHub

Got any Dapper Question?