Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of asynchronous processing of ADO.NET data

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the methods of asynchronous processing of ADO.NET data". In daily operations, I believe that many people have doubts about the methods of asynchronous processing of ADO.NET data. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the methods of asynchronous processing of ADO.NET data?" Next, please follow the editor to study!

Many development languages support asynchronous communication. In ADO.NET, there are providers that support asynchronous processing. When System.Data.SqlClient inserts updates for a large number of data, the ADO.NET data asynchronous processing method can be used without waiting for multiple data updates before operation or next processing. The SqlCommand object method improves the user experience as follows:

Asynchronous processing of ADO.NET data

◆ BeginExecuteNonQueryEndExecuteNonQuery

◆ BeginExecuteXmlReaderEndExecuteXmlReader

◆ BeginExecuteReaderEndExecuteReader

The method of ◆ begin prefix passes parameters, and the method of end prefix returns output parameters and return value

The ◆ begin prefix method returns the execution status of the one-step method used by IAsyncResult to track

◆ IAsyncResult.AsnycState user-defined status object

The waiting form of the ◆ IAsyncResult.AsnycWaitHandle call code, whether to wait for one of the asynchronous methods to complete or to complete

◆ IAsyncResult.CompletedSynchronously gets whether all asynchronous methods are completed at the same time

Whether the execution of ◆ IAsyncResult.Iscompleted is finished, you can perform the lower action based on this attribute.

Add "async=true" to the connection string. If all commands are synchronous, it is recommended to add them in the connection string. "async=false" if some commands are executed asynchronously and some are executed synchronously, it is recommended to establish two connection objects respectively. If "async=true", synchronous commands can also be executed, but some resources will be lost.

/ * * / 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 (nullroomroomorderid) {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 [nulls = = arDetails?2:3]; handles [0] = arCustomer.AsyncWaitHandle; handles [1] = arOrders.AsyncWaitHandle; if (nullable particulars details) handles [2] = arDetails.AsyncWaitHandle; / / waitforcommandstocompleteandrenderpagecontrolsaswe / / getdataback SqlDataReaderr; DataTabledt; for (intresults= (null==arDetails)? 1Pluto 0results

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report