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.