Enterprise Library step by step series (III): Data Access block-entry

Source: Internet
Author: User

Preface: the configuration application was originally mentioned earlier.ProgramBlock Design. Now I want to write the beginner and advanced sections of each application block, and then write the design article in a unified manner. I hope you can understand it :)

In this article, we will explain how to use data access application blocks to access data and provide relevant diagrams.

I.Data Access Application BlockWhat can we do?

1. Provides best practices;

2. Improve consistency;

3. Improve security;

4. Improve ease of use;

II.Data Access Application BlockUsage:

Here I willDaabIs summarized as a trilogy. Before starting, assume that a new project has been created andWeb. configOrApp. configConfiguration file.

Step 1 Define the Configuration:

1.RunEnterprise Library ConfigurationTools,SelectFile | open applicationOpenApp. configFile

2.Right-clickApplicationAnd selectNew | Data Access Application Block

 

3.We can see thatData Access Application BlockAt the same time,Configuration Application Block. Because all applications are based on configuring application blocks.

4.Modify the Database Name:

 

5.Modify the server name:

 

6.Right-clickSQL connection string, SelectParameterTo create a new parameter:

 

7.Modify the parameter nameUIDAnd modifyValue, Specified Login Name:

 

8.Use the same method to create anotherPassword parameter, NamePWD;ValueIs the logon password:

9.Finally, create a database instance:

 

10.SelectFile | save allCommand to save all:

11. Select Attribute|Generate events|Post-event command line Enter the following content:

Copy " $ (Projectdir) \ *. config "   " $ (Targetdir) "

Step 2 Create a database instance:

There are two ways to create a database instance: one is the default database instance, and the other is to map a named instance to the configuration file.

1 /**/ ///Create default instance
2 Database DB = Databasefactory. createdatabase ();
3
4 /**/ ///Create a database instance
5 Database DB = Databasefactory. createdatabase ( " Northwind " );

Step 3 RunSQLStatement:

DaabCan execute staticSQLStatement, or stored procedure.

 RelatedCodeAs follows:

1 /**/ /// <Summary>
2///Returned dataset type
3/// </Summary>
4 Private Dataset getdataset ()
5 {
6 Dataset DS = DB. executedataset (commandtype. Text, " Select * from employees " );
7
8 Return DS;
9 }
10
11 /**/ ///   <Summary>
12 /// Returns the datareader type.
13 ///   </Summary>
14 ///   <Returns> </returns>
15 Private Datareader getdatareader ()
16 {
17 Datareader Dr = DB. executereader (commandtype. Text, " Select * from employees " );
18
19 Return Dr;
20 }
21
22 /**/ ///   <Summary>
23 /// Returns a single value.
24 ///   </Summary>
25 ///   <Returns> </returns>
26 Private   Int Getsiglevalue ()
27 {
28 Int Icount = DB. executescalar (commandtype. Text, " Select count (*) from employees " );
29
30 Return Icount;
31 }

Summary: This article is just a simple example.DaabIn the next advanced article, I will write and useDaabExecutes stored procedures, parameter transmission, transaction support, and connection information encryption.

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.