The followingCodeEmbeddedSQLStatementBeginexecutereaderExtract the first five pieces of data andCallbackTo this method.No other processing is required. When the asynchronous call ends, the callback function is triggered and the result set is displayed on the screen.
<% @ Page Language ="C #"%>
<% @ Import namespace ="System. Data"%>
<% @ Import namespace ="System. Data. sqlclient"%>
<% @ Import namespace ="System. Configuration"%>
<SCRIPT runat ="Server">
Protected void page_load (Object sender, eventargs E)
{
Sqlconnection dbcon;
Sqlcommand command = new sqlcommand ();
Sqlasyncresult asyncresult;
Dbcon = new sqlconnection ();
Command = new sqlcommand ();
Dbcon. connectionstring =
Configurationmanager. connectionstrings ["Dsn_northwind"]. Connectionstring;
// Selecting top 5 records from the orders table
Command. commandtext =
"Select top 5 MERs. companyName, customers. contactname,"+
"Orders. orderid, orders. orderdate,"+
"Orders. requireddate, orders. shippeddate"+
"From orders, customers"+
"Where orders. customerid = customers. customerid"+
"Order by MERs. companyName, customers. contactname";
Command. commandtype = commandtype. text;
Command. Connection = dbcon;
Dbcon. open ();
// Starting the asynchronous Processing
Asyncresult = command. beginexecutereader (New asynccallback (cbmethod ),
Commandbehavior. closeconnection );
}
Public void cbmethod (sqlasyncresult AR)
{
Sqldatareader ordersreader;
// Retrieving result from the asynchronous Process
Ordersreader = ar. endexecutereader (AR );
// Displaying result on the screen
Gvorders. datasource = ordersreader;
Gvorders. databind ();
}
</SCRIPT>
The callback function allows you to process command execution results in other parts of the Code. This feature is very useful when the command execution process is generally longer, but you don't want users to wait until the process call ends.