Faithlife.Utility.Dapper 2.0.1

Prefix Reserved
dotnet add package Faithlife.Utility.Dapper --version 2.0.1
                    
NuGet\Install-Package Faithlife.Utility.Dapper -Version 2.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Faithlife.Utility.Dapper" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Faithlife.Utility.Dapper" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Faithlife.Utility.Dapper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Faithlife.Utility.Dapper --version 2.0.1
                    
#r "nuget: Faithlife.Utility.Dapper, 2.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Faithlife.Utility.Dapper@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Faithlife.Utility.Dapper&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Faithlife.Utility.Dapper&version=2.0.1
                    
Install as a Cake Tool

Faithlife.Utility.Dapper

Faithlife.Utility.Dapper is a utility library for Dapper.

NuGet

Overview

Faithlife.Utility.Dapper adds BulkInsert, a Dapper-like API for inserting many rows efficiently while still using command parameters.

Installation

Faithlife.Utility.Dapper should be installed via NuGet.

BulkInsert

The BulkInsert and BulkInsertAsync extension methods allow efficient insertion of many rows into a database table with a familiar Dapper-like API.

Problem

Dapper already has a mechanism for "bulk insert". Calling Execute with an IEnumerable will execute the specified INSERT command once for each item in the sequence. Unfortunately, this can be an extremely slow way to insert a large number of rows into a database.

Each DBMS has its own preferred approaches for efficiently inserting many rows into a database, but the most portable way is to execute an INSERT command with multiple rows in the VALUES clause, like so:

INSERT INTO widgets (name, size) VALUES ('foo', 22), ('bar', 14), ('baz', 42)

Building a SQL statement for a large number of rows is straightforward, but runs the risk of SQL injection problems if the SQL isn't escaped properly.

Using command parameters is safer, but building and executing the SQL is more complex. Furthermore, databases often have a limit on the maximum number of command parameters that can be used, so it can be necessary to execute multiple SQL statements, one for each batch of rows to insert.

Solution

BulkInsert is a simple Dapper-like extension method that builds the SQL commands for each batch and leverages Dapper to inject the command parameters.

var widgets = new[] { new { name = "foo", size = 22 }, new { name = "bar", size = 14 }, new { name = "baz", size = 42 } };
connection.BulkInsert("INSERT INTO widgets (name, size) VALUES (@name, @size) ...", widgets);

The ... after the VALUES clause must be included. It is used by BulkInsert to find the end of the VALUES clause that will be transformed. The call above will build a SQL statement like so:

INSERT INTO widgets (name, size) VALUES (@name_0, @size_0), (@name_1, @size_1)

The actual SQL statement will have as many parameters as needed to insert all of the specified rows. If the total number of command parameters exceeds 999 (the maximum number that SQLite supports and an efficient number for MySql.Data), it will execute multiple SQL commands until all of the rows are inserted.

All of the transformed SQL will be executed for each batch, so including additional statements before or after the INSERT statement is not recommended.

Execute the method within a transaction if it is important to avoid inserting only some of the rows if there is an error.

Reference

The BulkInsert and BulkInsertAsync methods of the BulkInsertUtility static class are extension methods on IDbConnection. They support these arguments:

  • sql: The SQL to execute, which must have a VALUES clause that is followed by ....
  • commonParam: The values of any command parameters that are outside the VALUES clause, or are common to every row. Optional.
  • insertParams: The values of the command parameters for each row to be inserted.
  • transaction: The current transaction, if any. See Dapper.Execute.
  • batchSize: If specified, indicates the number of rows to insert in each batch, even if doing so requires more than 999 command parameters.
  • cancellationToken: The optional cancellation token for BulkInsertAsync.

The method returns the total number of rows affected, or more specifically, the sum of the numbers returned when executing the SQL commands for each batch.

Contributing

See Contributing for setup and contribution guidelines. For a history of notable changes, see the Release Notes.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.1 1,330 5/26/2026
2.0.0 283,177 6/4/2024
1.1.0 430,243 4/22/2017
1.0.0 2,486 7/16/2016
0.1.3 1,713 7/13/2016