I. WHAT CAN Data Access Application Block do?
1. provide best practices;
2. Improve consistency;
3. Improve security;
4. Improved ease of use;
Ii. Use Data Access Application Block:
Here I will summarize the process of using daab as a trilogy. Before starting, assume that a new project has been created, and the Web. config or app. config configuration file has been created.
Step 1 defines the Configuration:
1.RunEnterprise Library ConfigurationTools,SelectFile | open applicationOpenApp. configFile
2.Right-click Application and selectNew | Data Access Application Block
3. We can see that while creating a data access application block, a configuration application block is also created. Because all applications are based on configuring application blocks.
4. Modify the Database Name:
5. Modify the server name:
6. Right-click SQL connection string and select parameter to create a new parameter:
7. Modify the parameter name to uid, modify the value, and specify the Login Name:
8. Create another password parameter in the same way and name it PWD. value indicates the logon password:
9. Create a database instance:
10. SelectFile | save allCommand to save all:
11. SelectAttribute|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 a default instance
2 database DB = databasefactory. createdatabase ();
3
4/** // create a database instance
5 database DB = databasefactory. createdatabase ("northwind ");
Step 3: Execute the SQL statement:
Daab can execute static SQL statements or stored procedures.
The related code is as follows:
1/** // <summary>
2 // return the 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 // return 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 // return 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}