Created
June 4, 2019 11:16
-
-
Save dsect/f6f980c523007f98cf9a748a93bbd661 to your computer and use it in GitHub Desktop.
This file contains 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
private void Copy(string sourceCommandText, string destinationTableName, IEnumerable<SqlBulkCopyColumnMapping> columnMappings) | |
{ | |
using (var sqlBulkCopy = new SqlBulkCopy(_destinationConnectionString, SqlBulkCopyOptions.Default)) | |
{ | |
const int timeout = 1200; | |
sqlBulkCopy.BulkCopyTimeout = timeout; | |
sqlBulkCopy.BatchSize = 50000; | |
sqlBulkCopy.DestinationTableName = destinationTableName; | |
sqlBulkCopy.NotifyAfter = 1000000; | |
sqlBulkCopy.SqlRowsCopied += SqlBulkCopy_SqlRowsCopied; | |
foreach (var columnMapping in columnMappings) | |
{ | |
sqlBulkCopy.ColumnMappings.Add(columnMapping); | |
} | |
using (var sourceConnection = new SqlConnection(_sourceConnectionString)) | |
{ | |
sourceConnection.Open(); | |
using (var sqlCommand = new SqlCommand(sourceCommandText, sourceConnection)) | |
{ | |
sqlCommand.CommandType = CommandType.Text; | |
sqlCommand.CommandTimeout = timeout; | |
using (var sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection)) | |
{ | |
sqlBulkCopy.WriteToServer(sqlDataReader); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment