Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One funny idea <code>SqlConnection</code> provides <code>InfoMessage</code> event which fires when server executes a <code>PRINT</code> command. You can print in some parts of your stored procedure and listen to this event.</p> <pre class="lang-sql prettyprint-override"><code>-- do something PRINT '10 PERCENT COMPLETED'; -- do another thing PRINT '20 PERCENT COMPLETED'; ... PRINT '100 PERCENT COMPLETED'; </code></pre> <p>other than that, use @hamlet-hakobyan 's solution. just show a infinite progress bar.</p> <hr> <h2>Update: Updated to include a full solution</h2> <p>First of all, I hate to give full answers. It prevents mind's ability to find a solution. Instead I like to nudge people into correct path, so they can walk the walk. But here it is anyway. Tested using VS2012, NET4, MSIL under W7x64SP1 and SQL2012.</p> <p>My very time consuming SP. Used <code>RaisError</code> instead of <code>Print</code> to send messages immediately. </p> <pre><code>Create Procedure usp_LongProcess As Begin Declare @i Int; Declare @msg VarChar(50); Set @i = 0; while (@i &lt; 100) Begin WaitFor Delay '00:00:02'; Set @i = @i + 10; Set @msg = Convert(VarChar(10), @i) + ' PERCENT COMPLETE'; RaisError(@msg, 1, 1) With NoWait End End </code></pre> <p>And my form with </p> <ul> <li>a button (<code>CallSpButton</code>)</li> <li>a progress bar (<code>progress</code>)</li> <li>a label (<code>statusLabel</code>) and </li> <li>a background worker (<code>SpCaller</code>) with <code>WorkerReportsProgress</code> set <code>true</code>.</li> </ul> <p><img src="https://i.stack.imgur.com/DcJqa.jpg" alt="Screenshot"></p> <p>And at last the code that makes the call</p> <pre><code>private void CallSpButton_Click(object sender, EventArgs e) { CallSpButton.Enabled = false; SpCaller.RunWorkerAsync(); } private void SpCaller_DoWork(object sender, DoWorkEventArgs e) { var self = (BackgroundWorker) sender; var cb = new SqlConnectionStringBuilder { DataSource = ".", InitialCatalog = "Sandbox", IntegratedSecurity = true }; using (var cn = new SqlConnection(cb.ToString())) { cn.FireInfoMessageEventOnUserErrors = true; cn.Open(); cn.InfoMessage += (o, args) =&gt; self.ReportProgress(0, args.Message); using (var cmd = cn.CreateCommand()) { cmd.CommandText = "usp_LongProcess"; cmd.CommandType = CommandType.StoredProcedure; cmd.ExecuteNonQuery(); } } } private void SpCaller_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { CallSpButton.Enabled = true; } private void SpCaller_ProgressChanged(object sender, ProgressChangedEventArgs e) { var message = Convert.ToString(e.UserState); Debug.WriteLine(message); statusLabel.Text = message; if (message.EndsWith(" PERCENT COMPLETE")) { int percent; if (int.TryParse(message.Split(' ')[0], out percent)) progress.Value = percent; } } </code></pre>
 

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