Skip to content

Commit 77b670b

Browse files
committed
Initial Async Support
1 parent ef76eda commit 77b670b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/EntityFrameworkCore.SqlServer.SimpleBulks/Extensions/DataTableExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57

68
namespace EntityFrameworkCore.SqlServer.SimpleBulks.Extensions;
79

@@ -51,6 +53,29 @@ public static void SqlBulkCopy(this DataTable dataTable, string tableName, IRead
5153
bulkCopy.WriteToServer(dataTable);
5254
}
5355

56+
public static async Task SqlBulkCopyAsync(this DataTable dataTable, string tableName, IReadOnlyDictionary<string, string> columnNameMappings, SqlConnection connection, SqlTransaction transaction, BulkOptions options = null, CancellationToken cancellationToken = default)
57+
{
58+
options ??= new BulkOptions()
59+
{
60+
BatchSize = 0,
61+
Timeout = 30,
62+
};
63+
64+
using var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, transaction)
65+
{
66+
BatchSize = options.BatchSize,
67+
BulkCopyTimeout = options.Timeout,
68+
DestinationTableName = $"{tableName}"
69+
};
70+
71+
foreach (DataColumn dtColum in dataTable.Columns)
72+
{
73+
bulkCopy.ColumnMappings.Add(dtColum.ColumnName, GetDbColumnName(dtColum.ColumnName, columnNameMappings));
74+
}
75+
76+
await bulkCopy.WriteToServerAsync(dataTable, cancellationToken);
77+
}
78+
5479
private static string GetDbColumnName(string columnName, IReadOnlyDictionary<string, string> columnNameMappings)
5580
{
5681
if (columnNameMappings == null)

0 commit comments

Comments
 (0)