您好,登錄后才能下訂單哦!
這篇文章主要介紹“ADO.NET數據異步處理的方法有哪些”,在日常操作中,相信很多人在ADO.NET數據異步處理的方法有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”ADO.NET數據異步處理的方法有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
很多開發語言都支持異步通信,在ADO.NET中支持異步處理的提供程序有System.Data.SqlClient在針對大批量數據插入過更新時,使用ADO.NET數據異步處理方法可以不用等待多有數據更新完畢才能操作或者進行下步處理,改善了用戶體驗SqlCommand對象方法如下:
ADO.NET數據異步處理
◆BeginExecuteNonQueryEndExecuteNonQuery
◆BeginExecuteXmlReaderEndExecuteXmlReader
◆BeginExecuteReaderEndExecuteReader
◆begin前綴的方法傳入參數,end前綴的方法返回輸出參數和返回值
◆begin前綴方法返回的是IAsyncResult用于追蹤一步方法的執行狀態
◆IAsyncResult.AsnycState用戶自定義的狀態對象
◆IAsyncResult.AsnycWaitHandle呼叫代碼的等待形式,是等其中的一個異步方法完成還是全部完成
◆IAsyncResult.CompletedSynchronously獲取所有異步方法是否同時完成
◆IAsyncResult.Iscompleted是否執行完畢,可以根據此屬性進行下部動作
在連接字符串中加入"async=true"如果所有的命令都是同步的建議在連接字符串中顯施加入,"async=false"如果有一部分命令是異步執行,又有一部分是同步同步執行,建議分別建立兩個連接對象,如果"async=true"時,也可以執行同步命令,但是會損失一些資源
/**/////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; } } }
到此,關于“ADO.NET數據異步處理的方法有哪些”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。