支持異步處理的提供程序有 System
異步方法 BeginExecuteNonQuery EndExecuteNonQuery
BeginExecuteXmlReader EndExecuteXmlReader
BeginExecuteReader EndExecuteReader
begin前綴的方法傳入參數
begin前綴方法返回的是IAsyncResult 用於追蹤一步方法的執行狀態
IAsyncResult
IAsyncResult
IAsyncResult
IAsyncResult
在連接字符串中加入
如果所有的命令都是同步的建議在連接字符串中顯施加入
如果有一部分命令是異步執行
如果
/**///// obtain connection strings from configuration files or
//// similar facility
//// NOTE: these connection strings have to include
//// example:
////
//string connstrAccouting = GetConnString(
//string connstrHR = GetConnString(
/**///// define two connection objects
//using (SqlConnection connAcc = new SqlConnection(connstrAccounting))
//using (SqlConnection connHumanRes = new SqlConnection(connstrHR))
//{
// // open the first connection
// connAcc
// // start the execution of the first query contained in the
// //
// SqlCommand cmdAcc = new SqlCommand(
// cmdAcc
// cmdAcc
// IAsyncResult arAcc = cmdAcc
// // at this point
// // the server
// // now open the second connection
// connHumanRes
// // start the execution of the second stored
// // the human
// SqlCommand cmdHumanRes = new SqlCommand(
// connHumanRes);
// cmdHumanRes
// IAsyncResult arHumanRes = cmdHumanRes
// // now both queries are running at the same time
// // at this point; more work can be done from this thread
// // can simply wait until both commands finish
// // wait
// SqlDataReader drAcc = cmdAcc
// SqlDataReader drHumanRes = cmdHumanRes
// // now we can render the results
// // web control
// // WebForms form
//}
string custid =
string orderid =
// NOTE: connection strings denoted by
//
string connstring =
// we
using (SqlConnection c
using (SqlConnection c
using (SqlConnection c
{
// get customer info
c
SqlCommand cmd
cmd
IAsyncResult arCustomer = cmd
// get orders
c
SqlCommand cmd
cmd
IAsyncResult arOrders = cmd
// get order detail if user picked an order
IAsyncResult arDetails = null;
SqlCommand cmd
if (null != orderid)
{
c
cmd
cmd
arDetails = cmd
}
// build the wait handle array for WaitForMultipleObjects
WaitHandle[] handles = new WaitHandle[null == arDetails ?
handles[
handles[
if (null != arDetails)
handles[
// wait for commands to complete and render page controls as we
// get data back
SqlDataReader r;
DataTable dt;
for (int results = (null == arDetails) ?
{
// wait for any handle
int index = WaitHandle
if (WaitHandle
throw new Exception(
switch (index)
{
case
r = cmd
if (!r
continue;
lblCustomerID
lblCompanyName
lblContact
r
break;
case
r = cmd
dt = new DataTable();
dt
dgOrders
dgOrders
r
break;
case
r = cmd
dt = new DataTable();
dt
dgDetails
dgDetails
r
break;
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11392.html