Review above
PreviousArticleBased on agileeas. NET platform basic library for application development-the article "using UDA to manipulate SQL statements" demonstrates how to use UDA for regular business operations. We can see two data manipulation procedures, one is the original operation process, and the other is the simplified data operation process. application developers do not have to worry about opening or closing databases. UDA completes these tasks for us, we only need to implement business-relatedCodeYou can.
Problem cause
Let's take a look at the data processing process we provided earlier:
Agileeas. the net platform provides two data processing processes for us to choose from. The difference between the two methods lies in whether application developers control the connection opening and closing, the first method is to enable and disable the application developer management connection, which is called the native data access method. The second method is to enable and disable the database automatically controlled by the UDA object, we call it a lazy method. Before executing the SQL statement, the UDA opens the connection. After executing the SQL statement, the UDA closes the connection, that is, in the quer and execute methods, including the Open and Close operations on the database.
In. the second method is recommended for application development on the. NET platform. However, the query method closes the connection after the SQL statement is executed, if idatareader is a type of return that must be connected online, the second method is not supported. At the same time, for transaction processing, a transaction contains multiple statements, the second method does not support transaction processing when the connection is automatically opened or closed.
Solution
In view of the above problems, we provide two solutions. First, we do not recommend using native mode to process data access, 2. UDA's idataaccessor provides two delegate Methods: delegatequery, transactionexecute, and their related overloading. delegatequery implements the delegate query in datareader access mode, and transactionexecute implements the transaction delegate, for more information about the two delegate methods and their delegate definitions, see agileeas. net and agileeas. net base library reference manual.
Delegated query example
Now let's use the delegated query to modify the idatareader query example in the previous article. First, we paste the native idatareader query:
/// <Summary> /// Demonstrate how to use idatareader. /// </Summary> /// <Remarks> /// The returned type of idatareader must be opened by the database, that is, manually managing the opening of the connection. /// </Remarks> Public Void Demegetdatareader () {idataaccessor accessor = udacontext. dataaccessor; accessor. dataconnection. open (); Try {Idatareader reader = Null ; Try {Reader = accessor. querydatareader (" Select top 30 * From DBO. Product "); // Reader = (idatareader) accessor. Query ("select top 30 from DBO. Product ",
resulttype. datareader); // Equivalent while (reader. read () { int COUNT = reader. fieldcount; for ( int I = 0; I
string . format ("
{0 }={ 1} \ t ", reader. getname (I),
Reader. isdbnull (I )? "Null": Reader. getvalue (I);} system. Console. Write ("\ N");}}Finally{If(Reader! =Null&&! Reader. isclosed) reader. Close ();}}Finally{Accessor. dataconnection. Close ();}}
Next we will use delegatequery to transform this method:
// // demo proxy query. // Public void demedelegatequery () {idataaccessor accessor = udacontext. dataaccessor; accessor. delegatequery ( New datareaderhandler ( This . internalgetdatareader),
" select top 30 * From DBO. product ") ;}< span style =" color: #808080 ">/// // proxy method. // // void internalgetdatareader (idatareader reader) { while (reader. read () { int COUNT = reader. fieldcount; for ( int I = 0; I
string . format ("
{0 }={ 1} \ t ", reader. getname (I),
Reader. isdbnull (I )? "Null": Reader. getvalue (I);} system. Console. Write ("\ N");}}
Is this method quite concise.
For the structure of the data table involved in this example, refer to the data table structure based on agileeas. NET platform basic library for application development-General description and data definition, for data object model definition files, documents, DDL scripts download: Workshop.
Link
Agileeas. NET platform development guide-series Directories
Introduction to agileeas. NET application development platform-Index
Step by step teach you how to use the agileeas. net base class library for application development-series directory
Agileeas. NET platform application development tutorial-case plan
Official website of agileeas. net
Agile Software Engineering Lab
QQ: 116773358