How do I set a progress bar when selecting a database ?? This problem caused me one afternoon, and finally found that asynchronous ADO. NET is relatively simple and easy to use.
I learned another trick.
Winform, you will know at a glance, just a few controls... No more. Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Data. sqlclient;
Namespace asynchronousadonet
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
Toolstripprogressbar1.enabled = false;
}
Public void binddate ()
{
// Asynchronous processing must be enabled in the connection string for asynchronous operations.
String strconnection = "Data Source =.; initial catalog = adventureworks;" +
"Integrated Security = true; asynchronous Processing = true ";
String strquery = "select productcategory_1. *, production. productcategory. *" +
"From production. productcategory inner join" +
"Production. productcategory as productcategory_1 on" +
"Production. productcategory. productcategoryid =" +
"Productcategory_1.productcategoryid ";
Using (sqlconnection conn = new sqlconnection (strconnection ))
{
Sqlcommand comm = new sqlcommand (strquery, Conn );
Iasyncresult asyncresult = NULL; // indicates the status of asynchronous operations
Try
{
Conn. open ();
Asyncresult = comm. beginexecutereader (commandbehavior. closeconnection );
Toolstripprogressbar1.enabled = true;
Toolstripstatuslabel1.text = "processing ";
While (! Asyncresult. iscompleted) // determines whether the asynchronous operation is complete.
{
// Maximun = 100, minimun = 0, step = 10, style = Blocks
If (toolstripprogressbar1.value = toolstripprogressbar1.maximum)
Toolstripprogressbar1.value = toolstripprogressbar1.minimum;
Toolstripprogressbar1.20.mstep ();
}
Sqldatareader reader = comm. endexecutereader (asyncresult );
Datatable dtresult = new datatable ();
Dtresult. Load (Reader );
If (dtresult! = NULL)
Datagridview1.datasource = dtresult;
// Set the progress bar and text
Toolstripprogressbar1.value = toolstripprogressbar1.maximum;
Toolstripprogressbar1.enabled = false;
Toolstripstatuslabel1.text = "completed ";
}
Catch (exception E)
{
MessageBox. Show (E. Message );
}
}
}
Private void btngetdate_click (Object sender, eventargs E)
{
Binddate ();
}
}
}