SqlBulkCopyHelper 1.0.0
dotnet add package SqlBulkCopyHelper --version 1.0.0
NuGet\Install-Package SqlBulkCopyHelper -Version 1.0.0
<PackageReference Include="SqlBulkCopyHelper" Version="1.0.0" />
<PackageVersion Include="SqlBulkCopyHelper" Version="1.0.0" />
<PackageReference Include="SqlBulkCopyHelper" />
paket add SqlBulkCopyHelper --version 1.0.0
#r "nuget: SqlBulkCopyHelper, 1.0.0"
#:package SqlBulkCopyHelper@1.0.0
#addin nuget:?package=SqlBulkCopyHelper&version=1.0.0
#tool nuget:?package=SqlBulkCopyHelper&version=1.0.0
SqlBulkCopyHelper
This library makes it possible to use IEnumerable<T> together with SqlBulkCopy, by wrapping the list of values in a DataReader.
This makes it possible to stream insert data and drastically reduce the memory footprint.
It's inspired by its Postgres counterpart PostgreSQLCopyHelper
Installing
To install SqlBulkCopyHelper, run the following command in the Package Manager Console:
PM> Install-Package SqlBulkCopyHelper
Basic Usage
The simplest way to use it is to have a predefined class that contains all columns you like to insert to an existing table or temp table.
public class TestData
{
public bool BoolColumn { get; set; }
public byte ByteColumn { get; set; }
public byte[] ByteArrayColumn{ get; set; }
public short ShortColumn { get; set; }
public int IntColumn { get; set; }
public long LongColumn { get; set; }
public decimal DecimalColumn { get; set; }
public double DoubleColumn { get; set; }
public DateTime DateTimeColumn { get; set; }
public Guid GuidColumn { get; set; }
public string StringColumn { get; set; }
public int? NullableIntColumn { get; set; }
public char CharColumn { get; set; }
}
Then you could just use one of the extensions methods directly on an SqlConnection.
var testData = new List<TestData>(); // You can have it as a list in memory or get it as an IEnumerable from another source
var connection = new SqlConnection("your connection string");
var numberOfRowsInserted = await connection.BulkInserAsync("dbo.MyTable", testData); // This is a helper method that basically use .MapAllPublicProperties() as in the example below
Save your data to a temptable for future processing
var helper = new SqlBulkCopyHelper<TestData>("#Test") // The name of the table you like to insert into
.MapAllPublicProperties() // Use the predifined mapping that maps all columns
.UseBracketQuoting() // To make sure that the create table script always add [] around the column names
.RemoveMap("LongColumn"); // Lets say we are not interessted in the LongColumn, but still like to use the automapping
await using var connection = new SqlConnection("your connection string");
await connection.OpenAsync();
var sql = helper.CreateTableScript(); // Created a "CREATE TABLE #Test" script with all the columns that was mapped
await connection.ExecuteAsync(sql);
await helper.BulkInsertAsync(connection, testData);
var result = connection.Query<TestData>("SELECT * FROM #Test").ToList(); // Here we could do an Insert/Update/Merge or something else with the data in #Test
await connection.CloseAsync();
Naming convention
MapAllPublicProperties will default just use PropertyInfo.Name
You can change the behavior by providing a function.
helper.MapAllPublicProperties(propertyInfo => propertyInfo.Name.ToLower());
It's also possible to use that function in the SqlConnection extension.
await connection.BulkInserAsync("#Test", testData, propertyInfo => propertyInfo.Name.ToLower());
Do you own mapping
There is a few mapping options, the simplest is just an expression:
var helper = new SqlBulkCopyHelper<Test>("dbo.Test")
.Map("BoolColumn", x => x.BoolColumn)
.Map("ByteColumn", x => x.ByteColumn);
await helper.BulkInsertAsync(connection, testData);
Since it's an expression you have some options, like concat a name:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
var helper = new SqlBulkCopyHelper<Person>("dbo.Person")
.Map("FirstName", x => x.FirstName)
.Map("LastName", x => x.LastName)
.Map("Fullname", x => string.Concat(x.FirstName, " ", x.LastName));
await helper.BulkInsertAsync(connection, persons);
You are not forced to have a class for the mapping. You could have just a list of integers.
var numbers = new List<int>();
var helper = new SqlBulkCopyHelper<int>("dbo.Test")
.Map("IntColumn"); // In this scenario you just have to map the column name
await helper.BulkInsertAsync(connection, numbers);
Dynamic
For example if you use Dapper without defining a class you get back DapperRow that you can cast to Dictionary<string, object> and use in the mapper.
var data = new List<Dictionary<string, object>>();
var helper = new SqlBulkCopyHelper<Dictionary<string, object>>("dbo.Test")
.Map("Id", x => x["Id"], typeof(int))
.Map("Name", x => x["Name"], typeof(string));
Or if you like to go the more reflection base way you can choose your own PropertyInfo for the mapping.
var properties = typeof(TestData).GetProperties().Where(x => x.PropertyType == typeof(int));
var helper = new SqlBulkCopyHelper<TestData>("#Test")
.MapProperties(properties);
The PropertyType method will convert the properties to an expression and automatically choose the property name as the database column name.
If you like to define you own column name you could provide a naming funtion:
var helper = new SqlBulkCopyHelper<TestData>("#Test")
.MapProperties(properties, propertyInfo => propertyInfo.Name.ToLower());
or the properties one by one
var helper = new SqlBulkCopyHelper<TestData>("#Test");
var properties = typeof(TestData).GetProperties().Where(x => x.PropertyType == typeof(int));
foreach (var property in properties)
{
helper.MapProperty(property, property.Name.ToLower());
}
I hope that I added enough helper methods to make it easy to create you own extensions methods that fit your use case.
DisguisedDataReader.cs
SqlBulkCopy only accept DataTable and DbDataReader for its input. DataTable forces you to load all the data into memory before inserting it into your database. DbDataReader makes it possible to stream insert data, but you have to implement the reader yourself or use a library for it. I needed to move a large amount of data between two databases and got memory problems with DataTable, that's why I started looking into other options. DisguisedDataReader will wrap your IEnumerable<T> into a DbDataReader making it possible to stream insert with SqlBulkCopy.
| Product | Versions 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. |
-
.NETStandard 2.0
- Microsoft.Data.SqlClient (>= 5.2.3)
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 |
|---|---|---|
| 1.0.0 | 79 | 2/26/2026 |