Last active
July 6, 2018 14:06
-
-
Save eeskildsen/62d623f9eec7ef5690b0bd1896e78805 to your computer and use it in GitHub Desktop.
Fluent extension to set a SqlBulkCopy instance's column mappings from
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 System.Collections.Generic; | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.Linq; | |
public static class SqlBulkCopyExtensions | |
{ | |
public static SqlBulkCopy WithColumnMappings(this SqlBulkCopy sqlBulkCopy, DataColumnCollection columns) => WithColumnMappings(sqlBulkCopy, columns.Cast<DataColumn>()); | |
public static SqlBulkCopy WithColumnMappings(this SqlBulkCopy sqlBulkCopy, IEnumerable<DataColumn> columns) | |
{ | |
sqlBulkCopy.ColumnMappings.Clear(); | |
foreach (DataColumn column in columns) | |
{ | |
sqlBulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName); | |
} | |
return sqlBulkCopy; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment