溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ADO.NET數(shù)據(jù)異步處理的方法有哪些

發(fā)布時(shí)間:2021-12-03 15:28:20 來(lái)源:億速云 閱讀:208 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹“ADO.NET數(shù)據(jù)異步處理的方法有哪些”,在日常操作中,相信很多人在ADO.NET數(shù)據(jù)異步處理的方法有哪些問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”ADO.NET數(shù)據(jù)異步處理的方法有哪些”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

很多開發(fā)語(yǔ)言都支持異步通信,在ADO.NET中支持異步處理的提供程序有System.Data.SqlClient在針對(duì)大批量數(shù)據(jù)插入過(guò)更新時(shí),使用ADO.NET數(shù)據(jù)異步處理方法可以不用等待多有數(shù)據(jù)更新完畢才能操作或者進(jìn)行下步處理,改善了用戶體驗(yàn)SqlCommand對(duì)象方法如下:

ADO.NET數(shù)據(jù)異步處理
BeginExecuteNonQueryEndExecuteNonQuery
◆BeginExecuteXmlReaderEndExecuteXmlReader
◆BeginExecuteReaderEndExecuteReader
◆begin前綴的方法傳入?yún)?shù),end前綴的方法返回輸出參數(shù)和返回值
◆begin前綴方法返回的是IAsyncResult用于追蹤一步方法的執(zhí)行狀態(tài)
◆IAsyncResult.AsnycState用戶自定義的狀態(tài)對(duì)象
◆IAsyncResult.AsnycWaitHandle呼叫代碼的等待形式,是等其中的一個(gè)異步方法完成還是全部完成
◆IAsyncResult.CompletedSynchronously獲取所有異步方法是否同時(shí)完成
◆IAsyncResult.Iscompleted是否執(zhí)行完畢,可以根據(jù)此屬性進(jìn)行下部動(dòng)作

在連接字符串中加入"async=true"如果所有的命令都是同步的建議在連接字符串中顯施加入,"async=false"如果有一部分命令是異步執(zhí)行,又有一部分是同步同步執(zhí)行,建議分別建立兩個(gè)連接對(duì)象,如果"async=true"時(shí),也可以執(zhí)行同步命令,但是會(huì)損失一些資源

/**/////obtainconnectionstringsfromconfigurationfilesor  ////similarfacility  ////NOTE:theseconnectionstringshavetoinclude"async=true",for  ////example:  ////"server=myserver;database=mydb;integratedsecurity=true;async=true"  //stringconnstrAccouting=GetConnString("accounting");  //stringconnstrHR=GetConnString("humanresources");  /**/////definetwoconnectionobjects,oneforeachdatabase  //using(SqlConnectionconnAcc=newSqlConnection(connstrAccounting))  //using(SqlConnectionconnHumanRes=newSqlConnection(connstrHR))  //{  ////openthefirstconnection  //connAcc.Open();  ////starttheexecutionofthefirstquerycontainedinthe  ////"employee_info"stored-procedure  //SqlCommandcmdAcc=newSqlCommand("employee_info",connAcc);  //cmdAcc.CommandType=CommandType.StoredProcedure;  //cmdAcc.Parameters.AddWithValue("@empl_id",employee_id);  //IAsyncResultarAcc=cmdAcc.BeginExecuteReader();  ////atthispoint,the"employee_info"stored-procisexecutingon  ////theserver,andthisthreadisrunningatthesametime  ////nowopenthesecondconnection  //connHumanRes.Open();  ////starttheexecutionofthesecondstored-procagainst  ////thehuman-resourcesserver  //SqlCommandcmdHumanRes=newSqlCommand("employee_hrinfo",  //connHumanRes);  //cmdHumanRes.Parameters.AddWithValue("@empl_id",employee_id);  //IAsyncResultarHumanRes=cmdHumanRes.BeginExecuteReader();  ////nowbothqueriesarerunningatthesametime  ////atthispoint;moreworkcanbedonefromthisthread,orwe  ////cansimplywaituntilbothcommandsfinish-inourcasewe'll  ////wait  //SqlDataReaderdrAcc=cmdAcc.EndExecuteReader(arAcc);  //SqlDataReaderdrHumanRes=cmdHumanRes.EndExecuteReader(arHumanRes);  ////nowwecanrendertheresults,forexample,bindthereaderstoanASP.NET  ////webcontrol,orscanthereaderanddrawtheinformationina  ////WebFormsform.  //}   stringcustid="ALFKI";  stringorderid="10643";   //NOTE:connectionstringsdenotedby"connstring"havetoinclude  //"async=true",forexample:  stringconnstring="server=(local);database=northwind;integratedsecurity=true;async=true";  //we'llusethreeconnectionsforthis  using(SqlConnectionc1=newSqlConnection(connstring))  using(SqlConnectionc2=newSqlConnection(connstring))  using(SqlConnectionc3=newSqlConnection(connstring))  {  //getcustomerinfo  c1.Open();  SqlCommandcmd1=newSqlCommand(  "SELECTCustomerID,CompanyName,ContactNameFROMCustomersWHERECustomerID=@id",c1);  cmd1.Parameters.Add("@id",SqlDbType.Char,5).Value=custid;  IAsyncResultarCustomer=cmd1.BeginExecuteReader();  //getorders  c2.Open();  SqlCommandcmd2=newSqlCommand("SELECT*FROMOrdersWHERECustomerID=@id",c2);  cmd2.Parameters.Add("@id",SqlDbType.Char,5).Value=custid;  IAsyncResultarOrders=cmd2.BeginExecuteReader();  //getorderdetailifuserpickedanorder  IAsyncResultarDetails=null;  SqlCommandcmd3=null;  if(null!=orderid)  {  c3.Open();  cmd3=newSqlCommand("SELECT*FROM[OrderDetails]WHEREOrderID=@id",c3);  cmd3.Parameters.Add("@id",SqlDbType.Int).Value=int.Parse(orderid);  arDetails=cmd3.BeginExecuteReader();  }  //buildthewaithandlearrayforWaitForMultipleObjects  WaitHandle[]handles=newWaitHandle[null==arDetails?2:3];  handles[0]=arCustomer.AsyncWaitHandle;  handles[1]=arOrders.AsyncWaitHandle;  if(null!=arDetails)  handles[2]=arDetails.AsyncWaitHandle;  //waitforcommandstocompleteandrenderpagecontrolsaswe  //getdataback  SqlDataReaderr;  DataTabledt;  for(intresults=(null==arDetails)?1:0;results<3;results++)  {  //waitforanyhandle,thenprocessresultsastheycome  intindex=WaitHandle.WaitAny(handles,5000,false);//5secs  if(WaitHandle.WaitTimeout==index)  thrownewException("Timeout");  switch(index)  {  case0://customerqueryisready  r=cmd1.EndExecuteReader(arCustomer);  if(!r.Read())  continue;  lblCustomerID.Text=r.GetString(0);  lblCompanyName.Text=r.GetString(1);  lblContact.Text=r.GetString(2);  r.Close();  break;  case1://ordersqueryisready  r=cmd2.EndExecuteReader(arOrders);  dt=newDataTable();  dt.Load(r);  dgOrders.DataSource=dt;//data-bindtotheordersgrid  dgOrders.Refresh();  r.Close();  break;  case2://detailsqueryisready  r=cmd3.EndExecuteReader(arDetails);  dt=newDataTable();  dt.Load(r);  dgDetails.DataSource=dt;//data-bindtothedetailsgrid  dgDetails.Refresh();  r.Close();  break;   }  }  }

到此,關(guān)于“ADO.NET數(shù)據(jù)異步處理的方法有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI