Created
August 20, 2021 22:31
-
-
Save leandromoh/92a4ac4efb5865287025f4bbdeb8a076 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using var bulkCopy = new SqlBulkCopy( | |
_connection, | |
SqlBulkCopyOptions.TableLock | | |
SqlBulkCopyOptions.FireTriggers | | |
SqlBulkCopyOptions.UseInternalTransaction, | |
null | |
); | |
bulkCopy.DestinationTableName = "table"; | |
bulkCopy.BatchSize = 150_000; | |
bulkCopy.BulkCopyTimeout = TimeSpan.FromMinutes(60).Seconds; | |
var propNames = records.FirstOrDefault().GetPropertiesOf(); | |
foreach (var prop in propNames) | |
bulkCopy.ColumnMappings.Add(prop, prop); | |
var dataTable = records.ToDataTable(); // morelinq | |
await bulkCopy.WriteToServerAsync(dataTable); | |
public static IEnumerable<string> GetPropertiesOf<T>(this T _) | |
{ | |
var props = typeof(T) | |
.GetProperties(BindingFlags.Public | BindingFlags.Instance) | |
.Select(x => x.Name) | |
.ToList(); | |
return props; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment