Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use <code>ROW_NUMBER</code> to partition the huge resultset into smaller chunks. Then you could report progress on every chunk. Therefore you could use a <code>BackGroundWorker</code> to update a <code>Label</code> and/or a <code>ProgressBar</code>. To determine the size of each group you can select the total-rowcount first. Use this as divisor for the the chunksize (1000 in the following sample):</p> <p>Here is a working approach that fills a <code>DataTable</code> and select only small groups from the toal result using <code>LINQ</code>:</p> <p>On button-click start the <code>BackGroundWorker</code>:</p> <pre><code>Private Sub SomeButton_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Me.BackgroundWorker1.RunWorkerAsync() End Sub </code></pre> <p>Handle the <code>DoWork</code> event to load the data:</p> <pre><code>Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim tblData As New DataTable() Dim totalCount = 0 Dim chunkSize As Int32 = 1000 Dim countSQL = "SELECT COUNT(*) FROM dbo.tabData" Dim dataSql = "WITH CTE AS(SELECT d.*, rn=ROW_NUMBER()OVER(ORDER BY d.idData) FROM dbo.tabData d) SELECT * FROM CTE WHERE RN BETWEEN @RowStart AND @RowEnd;" Using con As New SqlConnection(My.Settings.ConnectionString) Using cmdCount = New SqlCommand(countSQL, con) con.Open() totalCount = DirectCast(cmdCount.ExecuteScalar, Integer) End Using Dim chunks = Enumerable.Range(0, totalCount). GroupBy(Function(i) i \ chunkSize). Select(Function(grp, index) New With { .RowStart = grp.Min() + 1, .RowEnd = grp.Max() + 1, .GroupNum = index + 1 }) For Each chunk In chunks Using cmdData = New SqlCommand(dataSql, con) cmdData.Parameters.AddWithValue("@RowStart", chunk.RowStart) cmdData.Parameters.AddWithValue("@RowEnd", chunk.RowEnd) Using da = New SqlDataAdapter(cmdData) da.Fill(tblData) BackgroundWorker1.ReportProgress(Math.Ceiling(chunk.GroupNum * chunkSize / totalCount)) End Using End Using Next BackgroundWorker1.ReportProgress(100) ' all data loaded ' End Using End Sub </code></pre> <p>Update the label and/or the <code>ProgressBar</code> on every chunk and finally:</p> <pre><code>Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged Me.ProgressLabel.Text = e.ProgressPercentage &amp; " Percent loaded" Me.ProgressBar1.Value = e.ProgressPercentage End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted Me.ProgressLabel.Text = "100 Percent loaded. Finished." End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload