Ado. Net has three asynchronous Processing Methods: poll, wait, and function callback.
Poll method: This method starts the asynchronous process, and then selects the iasyncresult object to see when the process is completed.
Wait: This method associates the wait handle with an asynchronous process. Wait until all or part of the processes are completed so that they can be processed accordingly.
Function callback method: When the asynchronous call ends, the callback function is triggered and the result set is displayed on the screen.
I have never used asynchronous processing. Some people on the Internet say that it can be used when a large amount of data is used or when multiple processes are executed together. This requires further research.
Poll usage
UseCode.
Put a gridview on the page to fetch data.
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
< ASP: gridview ID = "Gvdata" Runat = "Server" >
</ ASP: gridview >
</ Div >
</ Form >
</ Body >
Use poll to retrieve data.
Using System. Data;
Using System. Data. sqlclient;
Using System. configuration;
Namespace Adoasyncdb
{
/// <Summary>
/// Poll Method
/// </Summary>
Public Partial Class Pollmethod: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
Usepollmethod ();
}
Private VoidUsepollmethod ()
{
Sqlconnection connection;
Sqlcommand= NewSqlcommand ();
Sqldatareader datareader;
Iasyncresult asyncresult;
Response. Write (" ProgramStarted ...... <Br/> " );
Connection = New Sqlconnection (configurationmanager. connectionstrings [ " Northwinddb " ]. Connectionstring );
Sqlcommand. commandtext = " Select companyName, contactname, city, postalcode from DBO. MERs " ;
Sqlcommand. commandtype = Commandtype. text;
Sqlcommand. Connection = Connection;
Connection. open ();
Datetime startdatetime = Datetime. now;
Asyncresult = Sqlcommand. beginexecutereader (commandbehavior. closeconnection );
While(! Asyncresult. iscompleted)
{
Response. Write ( " Retrieving information ...... <Br/> " );
Response. Flush ();
System. Threading. thread. Sleep ( 3000 );
}
Datareader = Sqlcommand. endexecutereader (asyncresult );
Datetime enddatetime = Datetime. now;
Timespan = Enddatetime. Subtract (startdatetime );
Response. Write ( " Information retrieval is complete. " + Timespan. milliseconds + " Millisecond " );
Response. Flush ();
Gvdata. datasource = Datareader;
Gvdata. databind ();
Connection. Close ();
}
}
}
Download Code: adoasyncdb.rar
ArticleSummary:
Asynchronous processing in ADO. Net-poll
Asynchronous processing in ADO. Net-Wait
Asynchronous processing in ADO. Net-function callback