Click here to Skip to main content
14,984,043 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello,

I have an insert SQl request.
I want to add a progressBar to display the progress of this request.
C#
try
{
    SqlCommand cmd = new SqlCommand("insert into TABLE(a,b,c)Values(select from Table a1,a2,a3)", Connexion.cnx_import);
    Connexion.cnx_import.Open();
    cmd.ExecuteNonQuery();
    Connexion.cnx_import.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Source);
    Send_exception("Erreur dans la Fonction:import  OFS " + ex.ToString());
    Connexion.cnx_import.Close();
}

thank you in advance.

What I have tried:

I try to run a "Select count (*)" query repeatedly and affect the result to the ProgressBar, but this solution is not reliable.
Posted
Updated 18-Apr-21 23:49pm
v2
Comments
Richard MacCutchan 17-Apr-21 5:46am
   
You have been a member of CodeProject long enough to know that this is an English language site.

Um. You can't really do that as you don't get any "progress" information from the SQL operation - it's a blocking call to ExecuteNonQuery, which means that the function doesn't return until SQL Server has finished the whole operation.

You could display a "Marquee" type progress bar, but unless you move the INSERT code onto a separate thread so it isn't executing on the UI thread (which would block the Marquee from getting any Paint events so your user wouldn't see any change anyway) then nothing useful would happen!

And generally speaking, INSERT is a pretty quick operation - I'd probably just use a "wait" cursor to indicate I'm busy instead.

If you really must show a progress bar, then look at the BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^] to move your code, and a Marquee style ProgressBar Class (System.Windows.Forms) | Microsoft Docs[^] - but it's a fair amount of work for very little user benefit in most cases.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900