Wanting to use a Progress Bar with SQLBulkCopy

progress-bar sqlbulkcopy vb.net

Question

Question on an app I'm developing using VB.NET. I'm wanting to use a progress bar to show the progress of my SqlBulkCopy procedure. I've looked up some ways and was wondering if using an AddHandler in my SqlBulkCopy procedure, using SqlRowsCopied is a possible way to calculate the progress. And if so, how would I implement this, as I'm a bit new to progress bars in VB.NET. Let me know if there's a better way of going around this, all answers are very appreciated! THANKS! Just a small snippet of my SqlBulkCopy being executed.

(FYI: Not showing anything I've attempted with the progress bar)

Progress bar is named toolStripProgressBar:

Dim copy As New SqlBulkCopy(con)

For Each dc As DataColumn In dtDataCopy.Columns
    copy.ColumnMappings.Add(dc.ColumnName, dc.ColumnName)
Next

copy.DestinationTableName = TableName
copy.BulkCopyTimeout = 360 
copy.BatchSize = 1000
copy.WriteToServer(dtDataCopy)
copy.Close()

Accepted Answer

As the documentation states, the SqlRowsCopied event is dependent on the NotifyAfter property, NOT the BatchSize property. You would set the Maximum of your ProgressBar to the total number of rows and the Step property to the same as the NotifyAfter value. Each time the SqlRowsCopied event is raised, you call PerformStep on the ProgressBar and it will increment by the number of rows that have been copied.

Alternatively, you can simply assign the e.RowsCopied value to the Value property of the ProgressBar. There is a code example provided in the MSDN documentation for the event. It's to be hoped that you have consulted the documentation before posting this question.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why