Enterprise library4.0 Data Access Application Block instance description

Source: Internet
Author: User

Enterprise Library 4.0-May 2008 is Microsoft patterns &
The new practices Enterprise Library version consists of a series of application blocks and a core feature (such as object creation and configuration file mechanism). All these are reusable components, they are designed to help developers cope with common challenges faced by enterprise development. Version 4.0 also adds a new application block-Unity Application
This new Application Block provides containers for object generation and dependency injection. In addition, some other new features are added in 4.0.

Next we will use the data access application block as an instance:

First, we need to reference two DLL files in the project:

Microsoft. Practices. enterpriselibrary. Common. dll;

Microsoft. Practices. enterpriselibrary. Data. dll;

Add the configuration file app. config or web. config to the project.

Right-click the config file and choose edit enterprise.
Open the interface configuration file editing tool in library Configuration:

After opening the editing tool

Set parameters of localsqlserver, for example:

Parameter description:

1. connectionstring: connection string.

2. providername: Database adapter type.

3. Name: name of the connection string.

Set Data Access
The defaultdatabase attribute value of application block is set to the name of localsqlserver you just set, for example:

So far, the most concise data configuration is complete. Next we will start to write our code.

1.
First, we create a database instance:

// Instantiate the database

Database DB = databasefactory. createdatabase ("northwinddb ");

2. Create a dbcommand object (1. CMD for execution of common SQL statements;
2. CMD of the stored procedure execution ):

// Create a dbcommand object for normal SQL statement execution

Dbcommand dbcmd = dB. getsqlstringcommand ("SQL statement ");

// Method 1 of creating the dbcommand object executed by the Stored Procedure

Dbcommand dbcmd = dB. getsqlstringcommand ("Stored Procedure name ");

Dbcmd. commandtype
= Commandtype. storedprocedure;

// Method 2

Dbcommand dbcmd = dB. getstoredproccommand ("Stored Procedure name ");

If you want to pass parameters for a stored procedure, add the following code:

DB. addinparameter (dbcmd,
"Orderid", dbtype. int32, "parameter value"); // "orderid" indicates the parameter name corresponding to the stored procedure, and the third parameter indicates the parameter type.

3.
Execute SQL statements or stored procedures

In general, we need to return dataset, datareader, and single value. To get these three types of results, we can add the following code:

Dataset DS =
DB. executedataset (dbcmd); // get Dataset

Idatareader DR =
DB. executereader (dbcmd); // get datareader

String singlevalue =
DB. executescalar (dbcmd). tostring (); // obtain a single value. You can set the type according to your needs.

Now, we can use the dataset, datareader, and single value we have obtained to perform other operations (display, print, etc ).

In addition to some of the common usage we listed above, we may also frequently use transactions in actual development. Below I will list the usage of transactions:

Dbcommand proccmd1 = NULL;


Dbcommand proccmd2 = NULL;


Using (idbconnection
Con = dB. createconnection ())


{

Con. open ();

Idbtransaction
TRAN = con. begintransaction ();

Try

{

// Execute two stored procedures

Proccmd1 =
DB. getstoredproccommand ("Stored Procedure 1 ");

Proccmd2 =
DB. getstoredproccommand ("Stored Procedure 2 ");


DB. addinparameter (proccmd2, "orderid ",
Dbtype. int32, 10248); // The fourth parameter is the value of the stored procedure parameter, and the second parameter is the name of the stored procedure parameter.

Tran. Commit ();

}

Catch
(Exception ee)

{

Tran. rollback ();

}

Finally

{

Con. Close ();

}

}

Now, the instance is finished. ^ Brave Chen

 

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.