C # How To asynchronously read data in the database and bind the UI

Source: Internet
Author: User

When the database is asynchronously read, some problems may occur during data binding, that is, the form interface cannot be closed, and the process can only be ended after the task is completed. For example, the following code

First, set the thread to update the UI

A2.checkforillegalcrossthreadcils = false; // a2 indicates the form name.

The following code retrieves data from the database and binds it

Private void button#click (object sender, EventArgs e)
{
SqlConnection con;
SqlCommand com;
Try
{
Con = new SqlConnection ("UID = sa; Password = 123; Initial Catalog = AD; Data Source = 192.168.1.1; Asynchronous Processing = true ");
Con. Open ();
Com = new SqlCommand ("select top 100 * from tb_user", con );
Com. BeginExecuteReader (new AsyncCallback (delDataBin), com );
}
Catch (Exception ex)
{
MessageBox. Show ("program error, Message:" + ex. Message );
}

}

Private void delDataBin (IAsyncResult ar)
{
If (ar. IsCompleted)
{
SqlCommand com = (SqlCommand) ar. AsyncState;
SqlDataReader dr = com. EndExecuteReader (ar );
DataTable dt = new DataTable ();
Dt. Load (dr );
Dr. Close ();

This. Maid = dt; // bind data

}
}

The binding is completed here. Run and check the effect. In fact, the form is suspended.

The following is implemented through Invoke:

First declare the delegate public delegate void updateDG (DataTable dt );

Then bind the DataGridView through dataBin.

Public void dataBin (DataTable dt)
{
DataGridView1.DataSource = dt;
Return;
}

The following method is called in the thread.

// Bind data
If (this. InvokeRequired)
{
UpdateDG ur = new updateDG (dataBin );
This. Invoke (ur, dt );
}

The complete code is as follows:

 

Private void button#click (object sender, EventArgs e)
{
SqlConnection con;
SqlCommand com;
Try
{
Con = new SqlConnection ("UID = sa; Password = 123; Initial Catalog = AD; Data Source = 192.168.1.1; Asynchronous Processing = true ");
Con. Open ();
Com = new SqlCommand ("select top 100 * from tb_user", con );
Com. BeginExecuteReader (new AsyncCallback (delDataBin), com );
}
Catch (Exception ex)
{
MessageBox. Show ("program error, Message:" + ex. Message );
}

}

Private void delDataBin (IAsyncResult ar)
{
If (ar. IsCompleted)
{
SqlCommand com = (SqlCommand) ar. AsyncState;
SqlDataReader dr = com. EndExecuteReader (ar );
DataTable dt = new DataTable ();
Dt. Load (dr );
Dr. Close ();

// This. Maid = dt; // bind data

If (this. InvokeRequired)
{
UpdateDG ur = new updateDG (dataBin );
This. Invoke (ur, dt );
}

Related Article

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.