Public String constr = configurationmanager. connectionstrings ["northwindconnectionstring"]. connectionstring;
Protected void page_load (Object sender, eventargs E)
...{
If (! Ispostback)
...{
Binddata ();
// Bindmultidata ();
// Bindmultidataarray ();
// Binddatawithcallback ();
}
}
// The poll method of asynchronous query, the most common method
Private void binddata ()
...{
String querystr = "select * from MERs ";
Using (sqlconnection con = new sqlconnection (constr ))
...{
Sqlcommand cmd = new sqlcommand (querystr, con );
Iasyncresult asynresult;
Sqldatareader da;
Try
...{
Con. open ();
// The begin method returns an iasyncresult object to check whether execution is complete.
Asynresult = cmd. beginexecutereader (commandbehavior. closeconnection );
While (! Asynresult. iscompleted)
...{
Response. Write ("Asynchronous query </BR> ");
Asynresult. asyncwaithandle. waitone (3000, true );
// System. Threading. thread. Sleep (10 );
}
DA = cmd. endexecutereader (asynresult );
Gridview1.datasource = da;
Gridview1.databind ();
}
Catch (exception ex)
...{
Response. Write (ex. Message );
}
}
}
// The wait method of asynchronous query. Multiple wait handles are used for asynchronous query. The result set must be waiting for all processes to complete the processing.
Private void bindmultidata ()
...{
String cusquerystr = "select * from MERs where companyName = 'alfreds futterkiste '";
String supquerystr = "Select customers. companyName, customers. contactname," +
"Orders. orderid, orders. orderdate," +
"Orders. requireddate, orders. shippeddate" +
"From orders, customers" +
"Where orders. customerid = customers. customerid" +
"And customers. companyName = 'alfreds futterkiste '" +
"Order by MERs. companyName, customers. contactname ";
Using (sqlconnection mycon = new sqlconnection (constr ))
...{
Sqlcommand cuscmd = new sqlcommand (cusquerystr, mycon );
Sqlcommand supcmd = new sqlcommand (supquerystr, mycon );
Sqldatareader cusdr;
Sqldatareader supdr;
Iasyncresult cusisynresult;
Iasyncresult supisynresult;
// Create a handle Array
System. Threading. waithandle [] whandles = new system. Threading. waithandle [2];
System. Threading. waithandle cushandle;
System. Threading. waithandle suphandle;
Mycon. open ();
Cusisynresult = cuscmd. beginexecutereader (commandbehavior. closeconnection );
Supisynresult = supcmd. beginexecutereader (commandbehavior. closeconnection );
Cushandle = cusisynresult. asyncwaithandle;
Suphandle = supisynresult. asyncwaithandle;
// Assign the wait handle to the handle Array
Whandles [0] = cushandle;
Whandles [1] = suphandle;
// Pass the array to the waitall method and wait until the asynchronous query is complete.
System. Threading. waithandle. waitall (whandles );
Cusdr = cuscmd. endexecutereader (cusisynresult );
Supdr = supcmd. endexecutereader (supisynresult );
Gridview1.datasource = cusdr;
Gridview1.databind ();
Gridview2.datasource = supdr;
Gridview2.databind ();
Mycon. Dispose ();
Cuscmd. Dispose ();
Supcmd. Dispose ();
}
}
// The waitany method is used to process the result set without waiting for all processes to complete.
Private void bindmultidataarray ()
...{
String cusquerystr = "select * from MERs where companyName = 'alfreds futterkiste '";
String supquerystr = "Select customers. companyName, customers. contactname," +
"Orders. orderid, orders. orderdate," +
"Orders. requireddate, orders. shippeddate" +
"From orders, customers" +
"Where orders. customerid = customers. customerid" +
"And customers. companyName = 'alfreds futterkiste '" +
"Order by MERs. companyName, customers. contactname ";
Using (sqlconnection mycon = new sqlconnection (constr ))
...{
Sqlcommand cuscmd = new sqlcommand (cusquerystr, mycon );
Sqlcommand supcmd = new sqlcommand (supquerystr, mycon );
Sqldatareader cusdr;
Sqldatareader supdr;
Iasyncresult cusisynresult;
Iasyncresult supisynresult;
System. Threading. waithandle [] whandles = new system. Threading. waithandle [2];
System. Threading. waithandle cushandle;
System. Threading. waithandle suphandle;
Int whindex;
Mycon. open ();
Cusisynresult = cuscmd. beginexecutereader (commandbehavior. closeconnection );
Supisynresult = supcmd. beginexecutereader (commandbehavior. closeconnection );
Cushandle = cusisynresult. asyncwaithandle;
Suphandle = supisynresult. asyncwaithandle;
Whandles [0] = cushandle;
Whandles [1] = suphandle;
For (INT I = 0; I <whandles. length; I ++)
...{
// Waitany does not have to wait until all asynchronous operations are completed.
Whindex = system. Threading. waithandle. waitany (whandles );
Switch (whindex)
...{
Case 0:
Cusdr = cuscmd. endexecutereader (cusisynresult );
Gridview1.datasource = cusdr;
Gridview1.databind ();
Break;
case 1:
supdr = supcmd. endexecutereader (supisynresult);
gridview2.datasource = supdr;
gridview2.databind ();
break;
}< BR >}
Mycon. Dispose ();
Cuscmd. Dispose ();
Supcmd. Dispose ();
}
}
// Implement Asynchronous query through callback
Private void binddatawithcallback ()
...{
String supquerystr = "Select customers. companyName, customers. contactname," +
"Orders. orderid, orders. orderdate," +
"Orders. requireddate, orders. shippeddate" +
"From orders, customers" +
"Where orders. customerid = customers. customerid" +
"And customers. companyName = 'alfreds futterkiste '" +
"Order by MERs. companyName, customers. contactname ";
Using (sqlconnection mycon = new sqlconnection (constr ))
...{
Sqlcommand supcmd = new sqlcommand (supquerystr, mycon );
Sqldatareader supdr;
Iasyncresult supisynresult;
Mycon. open ();
Asynccallback callback = new asynccallback (callbackmethod );
Supisynresult = supcmd. beginexecutereader (callback, supcmd, commandbehavior. closeconnection );
System. Threading. thread. Sleep (100 );
Mycon. Dispose ();
Supcmd. Dispose ();
}
}
// Callback Method
Public void callbackmethod (iasyncresult iresult)
...{
Sqlcommand command = (sqlcommand) iresult. asyncstate;
Sqldatareader DR = command. endexecutereader (iresult );
Gridview1.datasource = Dr;
Gridview1.databind ();
}
}
This article from: China self-learning Programming Network (www.zxbc.cn) detailed source reference: http://www.zxbc.cn/html/adonet/1713241149778_2.html
Never give up: AdvancedProgramMember, website architect. He has been engaged in software development for many years and undertakes B/S architecture-related projects. If you are interested, contact QQ: 20028205