Skip to content

Commit

Permalink
Options.NotifyAfter fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
ttossavainen committed Sep 10, 2024
1 parent 6ccdece commit d75f2b6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Frends.MicrosoftSQL.BulkInsert/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.2.0] - 2024-09-10
### Changed
- Updated Options.NotifyAfter property to be set dynamically based on the total row count, with a minimum value of 1, ensuring rowsCopied is updated correctly.

## [2.1.0] - 2024-08-26
### Changed
- Updated Newtonsoft.Json to the latest version 13.0.3.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,21 @@ private static async Task<long> ExecuteHandler(Options options, string tableName
sqlBulkCopy.BulkCopyTimeout = options.CommandTimeoutSeconds;
sqlBulkCopy.DestinationTableName = tableName;
sqlBulkCopy.SqlRowsCopied += (s, e) => rowsCopied = e.RowsCopied;
sqlBulkCopy.NotifyAfter = options.NotifyAfter;

await sqlBulkCopy.WriteToServerAsync(dataSet.Tables[0], cancellationToken).ConfigureAwait(false);
// Calculate the number of rows and set value for NotifyAfter
var rowCount = dataSet.Tables[0].Rows.Count;
sqlBulkCopy.NotifyAfter = rowCount > 0 ? Math.Max(1, rowCount / 10) : 1;

return rowsCopied;
await sqlBulkCopy.WriteToServerAsync(dataSet.Tables[0], cancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex)
{
var notifyRange = rowsCopied + (options.NotifyAfter - 1);
throw new Exception($"ExecuteHandler exception, procecced row count between: {rowsCopied} and {notifyRange} (see Options.NotifyAfter). {ex}");
var notifyRange = rowsCopied + (sqlBulkCopy.NotifyAfter - 1);
throw new Exception($"ExecuteHandler exception, processed row count between: {rowsCopied} and {notifyRange} (see NotifyAfter). {ex}");
}

return rowsCopied;
}

private static void SetEmptyDataRowsToNull(DataSet dataSet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class Options
public int CommandTimeoutSeconds { get; set; }

/// <summary>
/// Defines the number of rows to be processed before generating a notification event. Range: 0 - 'count of rows to be processed'
/// Notification event can be used for error handling to see approximately which row the error happened.
/// Default value 0 = There won't be any notifications until the task is completed.
/// 10 = The counter is updated after every 10 rows or when every row has been processed.
/// Defines the number of rows to be processed before generating a notification event.
/// If the number of rows is unknown, NotifyAfter is dynamically set to 10% of the total row count, with a minimum value of 1.
/// Notification events can be used for error handling to see approximately which row the error occurred.
/// Default value 0 = There won't be any notifications until the task is completed and Result.Count will be 0.
/// </summary>
/// <example>0</example>
public int NotifyAfter { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down

0 comments on commit d75f2b6

Please sign in to comment.