Asynchronous ADO. Net operations

Source: Internet
Author: User
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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.