public async Task Execute(DataTable dataToCopy, string destinationTableName) { _dbConnection.Init(); var transaction = _dbConnection.BeginTransactionIfUsingInternalTransaction(); try { var mapping = _appContext.DataTableMappingCollection.GetMappingFor(dataToCopy.TableName); var columnNames = dataToCopy.Columns.Cast <DataColumn>() .Select(x => x.ColumnName) .ToArray(); using var bulkCopyCommand = _dbConnection.CreateNewBulkCopyCommand((SqlTransaction)transaction); bulkCopyCommand.MapColumns(mapping, columnNames); await bulkCopyCommand.WriteToServerAsync(dataToCopy, destinationTableName); transaction.CommitIfUsingInternalTransaction(_appContext.IsUsingExternalTransaction); transaction.DisposeIfUsingInternalTransaction(_appContext.IsUsingExternalTransaction); _dbConnection.DisposeIfUsingInternalTransaction(); } catch { transaction.Rollback(); throw; } }
public async Task Execute <T>(IEnumerable <T> dataToProcess, string destinationTableName) where T : class { _dbConnection.Init(); var transaction = _dbConnection.BeginTransactionIfUsingInternalTransaction(); try { var type = typeof(T); var mapping = _appContext.ModelMappingCollection.GetMappingFor(type); var propertyNames = type.GetPublicPropertyNames(); using var bulkCopyCommand = _dbConnection.CreateNewBulkCopyCommand((SqlTransaction)transaction); bulkCopyCommand.MapColumns(mapping, propertyNames); await bulkCopyCommand.WriteToServerAsync(dataToProcess, propertyNames, destinationTableName); transaction.CommitIfUsingInternalTransaction(_appContext.IsUsingExternalTransaction); transaction.DisposeIfUsingInternalTransaction(_appContext.IsUsingExternalTransaction); _dbConnection.DisposeIfUsingInternalTransaction(); } catch { transaction.Rollback(); throw; } }