Last Updated: 14 April 2020
This library will bulk insert the passed collection of POCOs into a SQL Server table, by converting it into an intermediate DataTable. The SQL table must exist, and it is assumed that the column names match the POCO's properties.
public class DemoRepository : IBulkInsertable<DemoObject>
{
private readonly IBulkInserter _bulkInserter;
private readonly string _tableName;
public DemoRepository(IBulkInserter bulkInserter, string tableName)
{
_bulkInserter = bulkInserter;
_tableName = tableName;
}
public Task BulkInsert(IReadOnlyList<DemoObject> items)
{
var table = items.ToDataTable();
if (table != null && table.Rows.Count > 0)
{
return _bulkInserter.InsertAsync(table, _tableName);
}
return Task.FromResult(0); // Obviously you'd want to await the InsertAsync call above
}
}
public class DemoObject
{
public int Id { get; set; }
public string Anything { get; set; }
}