With FastMember, you can do this without ever needing to go via DataTable
(which, in my tests, more-than-doubles the performance):
using(var bcp = new SqlBulkCopy(connection))
using(var reader = ObjectReader.Create(data, "Id", "Name", "Description"))
{
bcp.DestinationTableName = "SomeTable";
bcp.WriteToServer(reader);
}
Note that ObjectReader
can also work with non-generic sources, and it is not necessary to specify the member-names in advance (although you probably want to use the ColumnMappings
aspect of SqlBulkCopy
if you don’t specify them in the ObjectReader
itself).