/// <summary> /// Bulk insert an instance of <see cref="DbDataReader"/> object into the database in an asynchronous way. /// </summary> /// <param name="repository">The instance of <see cref="DbRepository{TDbConnection}"/> object.</param> /// <param name="tableName">The target table for bulk-insert operation.</param> /// <param name="dataTable">The <see cref="DataTable"/> object to be used in the bulk-insert operation.</param> /// <param name="rowState">The state of the rows to be copied to the destination.</param> /// <param name="mappings">The list of the columns to be used for mappings. If this parameter is not set, then all columns will be used for mapping.</param> /// <param name="options">The bulk-copy options to be used.</param> /// <param name="batchSize">The size per batch to be used.</param> /// <param name="transaction">The transaction to be used.</param> /// <returns>The number of rows affected by the execution.</returns> public static async Task <int> BulkInsertAsync(this DbRepository <SqlConnection> repository, string tableName, DataTable dataTable, DataRowState rowState = DataRowState.Unchanged, IEnumerable <BulkInsertMapItem> mappings = null, SqlBulkCopyOptions options = SqlBulkCopyOptions.Default, int?batchSize = null, SqlTransaction transaction = null) { // Create a connection var connection = (transaction?.Connection ?? repository.CreateConnection()); try { // Call the method return(await connection.BulkInsertAsync(tableName : tableName, dataTable : dataTable, rowState : rowState, mappings : mappings, options : options, bulkCopyTimeout : repository.CommandTimeout, batchSize : batchSize, transaction : transaction)); } catch { // Throw back the error throw; } finally { // Dispose the connection repository.DisposeConnectionForPerCall(connection, transaction); } }
/// <summary> /// Bulk insert a list of data entity objects into the database. /// </summary> /// <typeparam name="TEntity">The type of the data entity object.</typeparam> /// <param name="repository">The instance of <see cref="DbRepository{TDbConnection}"/> object.</param> /// <param name="tableName">The target table for bulk-insert operation.</param> /// <param name="entities">The list of the data entities to be bulk-inserted.</param> /// <param name="mappings">The list of the columns to be used for mappings. If this parameter is not set, then all columns will be used for mapping.</param> /// <param name="options">The bulk-copy options to be used.</param> /// <param name="batchSize">The size per batch to be used.</param> /// <param name="transaction">The transaction to be used.</param> /// <returns>The number of rows affected by the execution.</returns> public static int BulkInsert <TEntity>(this DbRepository <SqlConnection> repository, string tableName, IEnumerable <TEntity> entities, IEnumerable <BulkInsertMapItem> mappings = null, SqlBulkCopyOptions options = SqlBulkCopyOptions.Default, int?batchSize = null, SqlTransaction transaction = null) where TEntity : class { // Create a connection var connection = (transaction?.Connection ?? repository.CreateConnection()); try { // Call the method return(connection.BulkInsert <TEntity>(tableName: tableName, entities: entities, mappings: mappings, options: options, bulkCopyTimeout: repository.CommandTimeout, batchSize: batchSize, transaction: transaction)); } catch { // Throw back the error throw; } finally { // Dispose the connection repository.DisposeConnectionForPerCall(connection, transaction); } }
/// <summary> /// Bulk insert an instance of <see cref="DbDataReader"/> object into the database in an asynchronous way. /// </summary> /// <typeparam name="TEntity">The type of the data entity object.</typeparam> /// <param name="repository">The instance of <see cref="DbRepository{TDbConnection}"/> object.</param> /// <param name="reader">The <see cref="DbDataReader"/> object to be used in the bulk-insert operation.</param> /// <param name="mappings">The list of the columns to be used for mappings. If this parameter is not set, then all columns will be used for mapping.</param> /// <param name="options">The bulk-copy options to be used.</param> /// <param name="batchSize">The size per batch to be used.</param> /// <param name="transaction">The transaction to be used.</param> /// <returns>The number of rows affected by the execution.</returns> public static async Task <int> BulkInsertAsync <TEntity>(this DbRepository <SqlConnection> repository, DbDataReader reader, IEnumerable <BulkInsertMapItem> mappings = null, SqlBulkCopyOptions options = SqlBulkCopyOptions.Default, int?batchSize = null, SqlTransaction transaction = null) where TEntity : class { // Create a connection var connection = (transaction?.Connection ?? repository.CreateConnection()); try { // Call the method return(await connection.BulkInsertAsync <TEntity>(reader : reader, mappings : mappings, options : options, bulkCopyTimeout : repository.CommandTimeout, batchSize : batchSize, transaction : transaction)); } catch { // Throw back the error throw; } finally { // Dispose the connection repository.DisposeConnectionForPerCall(connection); } }