Dapper ORM Extend your IDbConnection interface
with awesome extensions!

string sql = "SELECT * FROM Invoices WHERE CustomerID = @CustomerID";

using (var conn = My.ConnectionFactory())
{
    var invoices = conn.Query<Invoice>(sql, new { customerID} );
}

Is Dapper an ORM?

Is Dapper an ORM?

Yes and no! People are still arguing about it. Dapper has earned the title of king of the C# Micro ORM but is considered by multiple people as a simple object mapper for .NET.

Is Dapper better than Entity Framework?

Is Dapper better than Entity Framework?

Yes and no! People will prefer Dapper when they want to write the SQL query themselves with optimal performance. See a Dapper vs EF Core comparisons

Is Dapper SQL Injections safe?

Is Dapper SQL Injections safe?

Yes, it's 100% safe if you use parametrized queries as you should always do!

Does Dapper support Bulk Insert?

Does Dapper support Bulk Insert?

No, but a popular third-party library does: Dapper Plus. It's a good example of Dapper's extensibility.

Learn More
Does Dapper support my database provider?

Does Dapper support my database provider?

Probably yes since Dapper provides extensions to the IDbConnection interface. It's your job to write the SQL compatible with your database provider.

Does Dapper support Transaction?

Does Dapper support Transaction?

Yes, Dapper supports transactions both within a TransactionScope and through use of an optional parameter.


Dapper - Execute

Execute a command one or multiple times and return the number of affected rows.

Dapper - Execute Example
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
      var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"});

      Console.WriteLine(affectedRows);
   
      var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
   
      FiddleHelper.WriteTable(customer);
}

Try it

Dapper - Query

Execute a query and map the result.

Dapper - Query Example
string sql = "SELECT TOP 10 * FROM OrderDetails";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
      var orderDetails = connection.Query<OrderDetail>(sql).ToList();
      
      FiddleHelper.WriteTable(orderDetails);
}

Try it

Dapper - Parameters

Use parameter in your Dapper query.

Dapper - Parameter Example
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{

      var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"});

      Console.WriteLine(affectedRows);
   
      var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
      
      FiddleHelper.WriteTable(customer);
}

Try it

Dapper - Result

Map the query result to different types.

Dapper - Result Example
string sql = "SELECT TOP 10 * FROM OrderDetails";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{   
      var orderDetails = connection.Query<OrderDetail>(sql).ToList();

      Console.WriteLine(orderDetails.Count);
      
      FiddleHelper.WriteTable(orderDetails);
}

Try it