Entlib 3.1 Study Notes (2): Data Access Application Block

Source: Internet
Author: User

 

Simplify development tasks for universal data access. ApplicationProgramApplication blocks can be used in many cases, such as reading display data, obtaining data through the application layer, and submitting changed data back to the database system. Application blocks support stored procedures, embedded SQL, and common internal processing tasks (such as managing connections, creating and caching parameters encapsulated in the methods of application blocks. In other words, the Data Access Application Block provides access to the most common ADO. NET functions.

1. Solved problems:
(1) It can reduce the number of samples writtenCodeTo perform standard tasks.
(2) It helps maintain consistent data access practices in the application and the entire enterprise.
(3) It can reduce the difficulty of changing the physical database target.
(4) It frees developers from learning different programming models for different types of databases.
(5) porting an application to different types of databases can reduce the number of codes that need to be rewritten.

2. Use:
(1) Use Enterprise Library configuration to open the application configuration file.
(2) Add reference: Microsoft. Practices. enterpriselibrary. Data. dll
Using Microsoft. Practices. enterpriselibrary. Data;
(3) create a database connection operation class:

Database DB = Databasefactory. createdatabase ( " Northwind " );

(4) execute the SQL statement:

Dataset DS = DB. executedataset (commandtype. Text, commandtext );
Datareader Dr = DB. executereader (commandtype. Text, commandtext); remember to use dr. Close ();
Object Result = DB. executescalar (commandtype. Text, commandtext );
// Or
Dbcommand = DB. getsqlstringcommand (commandtext );
Object Result = DB. Execute * (Dbcommand );

(5) execute the stored procedure:

Dbcommand = DB. getstoredproccommand (storeprocname );
DB. addinparameter (dbcommand, " Inparam " , Dbtype. int32, category ); // Note that @ is not added here and below, and daab is automatically added.
DB. addoutparameter (dbcommand, " Outparam " , Dbtype. int32, category );
Object Result = DB. Execute * (Dbcommand );
Object Outvalue = DB. getparametervalue (dbcommand, " Outparam " );

(6) Update Dataset:

Dbcommand insertcommand = DB. getstoredproccommand ( " Addcommand " ); // You can add processing parameters again, as shown below
Dbcommand deletecommand = DB. getstoredproccommand ( " Deletecommand " );
Dbcommand updatecommand = DB. getstoredproccommand ( " Updatecommand " );
Int Rowsaffected = DB. updatedataset (dataset, " Tablename " , Insertcommand, updatecommand,
Deletecommand, updatebehavior. Standard );

(7) handling transactions:

Public   Bool Transfer ( Int Transactionamount, Int Sourceaccount, Int Destinationaccount)
{
Bool Result =   False ;
Database DB = Databasefactory. createdatabase ();

String Sqlcommand =   " Command1 " ;
Dbcommand creditcommand = DB. getstoredproccommand (sqlcommand );

Sqlcommand =   " Comand2 " ;
Dbcommand debitcommand = DB. getstoredproccommand (sqlcommand );

Using (Dbconnection connection = DB. createconnection ())
{
Connection. open ();
Dbtransaction transaction = Connection. begintransaction ();
Try
{
DB. executenonquery (creditcommand, transaction );
DB. executenonquery (debitcommand, transaction );
Transaction. Commit ();
Result= True;
}
Catch
{
Transaction. rollback ();
}
Connection. Close ();
Return Result;
}
}

3. Data Access Application Block Design:

4. I haven't figured it out yet:

Dbcommand = DB. getstoredproccommand (commandtest / Storeprocname, productid );
Object Value = DB. executescalar (dbcommand );

Passing the productid value to the commandtest/storeprocname results in parameter diseter being used to correctly establish the parameter information for the productid. subsequent callto this method will cause the block to retrieve the parameter information from the cache, and not require rediscovery.
Http://davidhayden.com/blog/dave/archive/2006/11/03/CachingStoredProcedureParameters.aspx
Http://davidhayden.com/blog/dave/archive/2006/11/01/SqlCommandBuilderDeriveParameters.aspx
It seems that the parameter is being cached or something, but I don't know what the purpose is? Can we really improve performance? -_-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.