Bulk Operations in Dapper for .NET Core
Bulk Operations in Dapper for .NET
Core
Dapper is a high-performance
micro-ORM that provides efficient ways to interact with databases. While it
excels at executing simple queries, it also supports bulk operations, allowing
developers to perform batch inserts, updates, and deletions efficiently. In
this article, we'll explore how to implement bulk operations in Dapper and why
they are crucial for optimizing database interactions.
Why Use Bulk Operations?
When dealing with large datasets,
executing multiple single-row operations can lead to performance bottlenecks
due to frequent database round trips. Bulk operations help to:
- Improve Performance:
Reduce the number of database calls by processing multiple rows in a
single query.
- Reduce Overhead:
Minimize the overhead associated with multiple INSERT, UPDATE, or DELETE
statements.
- Enhance Scalability:
Allow efficient handling of large volumes of data.
Bulk Insert Using Dapper
Dapper does not provide native
bulk insert support like Entity Framework’s BulkInsert(), but you can achieve
similar functionality using SqlBulkCopy or optimized multi-row inserts.
Using SqlBulkCopy for
High-Performance Bulk Insert
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Dapper;
public class EmployeeRepository
{
private
readonly string _connectionString;
public
EmployeeRepository(string connectionString)
{
_connectionString
= connectionString;
}
public
void BulkInsertEmployees(IEnumerable<Employee> employees)
{
using
(var connection = new SqlConnection(_connectionString))
{
connection.Open();
using
(var bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.DestinationTableName
= "Employees";
var
table = new DataTable();
table.Columns.Add("Name",
typeof(string));
table.Columns.Add("Email",
typeof(string));
table.Columns.Add("Salary",
typeof(decimal));
table.Columns.Add("Department",
typeof(string));
foreach
(var emp in employees)
{
table.Rows.Add(emp.Name,
emp.Email, emp.Salary, emp.Department);
}
bulkCopy.WriteToServer(table);
}
}
}
}
Benefits of Using SqlBulkCopy
- Significantly faster than
executing multiple INSERT statements.
- Efficient memory usage when
handling large data sets.
- Direct mapping of
data to a database table.
Bulk Update and Delete Using
Dapper
Unlike bulk insert, Dapper does
not provide built-in bulk update/delete methods, but we can achieve this using
efficient SQL queries with Execute().
Bulk Update with Dapper
public void BulkUpdateEmployees(IEnumerable<Employee>
employees)
{
using (var
connection = new SqlConnection(_connectionString))
{
connection.Open();
var
query = "UPDATE Employees SET Salary = @Salary, Department = @Department
WHERE Email = @Email";
connection.Execute(query,
employees);
}
}
Bulk Delete with Dapper
public void
BulkDeleteEmployees(IEnumerable<int> employeeIds)
{
using (var
connection = new SqlConnection(_connectionString))
{
connection.Open();
var
query = "DELETE FROM Employees WHERE Id IN @Ids";
connection.Execute(query,
new { Ids = employeeIds });
}
}
Benefits of Using Bulk
Update/Delete
- Batches updates and deletions into
a single query execution.
- Reduces database round trips,
leading to faster execution.
- Maintains database consistency with
parameterized queries.
Conclusion
Bulk operations in Dapper can
drastically improve database performance by minimizing the number of database
calls and processing large datasets efficiently. Using SqlBulkCopy for inserts
and batch updates/deletes with Execute() ensures high performance and
scalability.
If you need even more advanced
bulk operations, consider third-party libraries like Dapper.Bulk that extend
Dapper’s functionality.
Comments
Post a Comment