写在前面:本来在前面说要写配置应用程序块的设计篇,现在我想先把各个应用程序块的入门篇和进阶篇写完后,再统一写设计篇,希望大家理解:)
在本篇随笔里面,一步一步说明怎么去用数据访问应用程序块来进行数据的访问,并给出相关的图示。
一.Data Access Application Block能够做什么?
1.提供最佳实践;
2.改善一致性;
3.改善安全性;
4.改善易用性;
二.Data Access Application Block使用:
这里我把DAAB的使用过程总结为三部曲。在开始前我们假设已经有一个新建好的项目,并且有Web.config或App.config配置文件。
第一步 定义配置:
1.运行Enterprise Library Configuration 工具,选择File | Open Application 打开App.config文件
2.右击Application,并选择New | Data Access Application Block
3.我们看到,在创建了一个Data Access Application Block的同时,也创建了一个Configuration Application Block。因为所有的应用程序快都是以配置应用程序块为基础的。
4.修改数据库的名称:
5.修改服务器的名称:
6.右击Sql Connection String,选择Parameter,创建一个新参数:
7.修改参数的名称为Uid,并修改Value,指定登录名:
8.用同样的方法再创建一个Password Parameter,取名Pwd;Value为登录密码:
9.最后创建一个数据库的实例:
10.选择File | Save All命令保存全部:
11.在项目中选择 属性|生成事件|生成后事件命令行 输入如下内容:
![None.gif](/Images/OutliningIndicators/None.gif)
第二步 创建数据库实例:
创建数据库实例有两种方法,一是默认的数据库实例,一是用命名实例映射到配置文件。
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
5
![None.gif](/Images/OutliningIndicators/None.gif)
第三步 执行SQL语句:
DAAB可以执行静态的SQL语句,或者存储过程等。
相关代码如下:
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
2
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
3
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![222530190136c9c4cfd237cc0d5cff99.jpe](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![None.gif](/Images/OutliningIndicators/None.gif)
11
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
14
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
15
![None.gif](/Images/OutliningIndicators/None.gif)
16
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![222530190136c9c4cfd237cc0d5cff99.jpe](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
17
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
20
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
21
![None.gif](/Images/OutliningIndicators/None.gif)
22
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
23
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
24
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
25
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
26
![None.gif](/Images/OutliningIndicators/None.gif)
27
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![222530190136c9c4cfd237cc0d5cff99.jpe](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
28
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
29
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
31
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
总结,本文只是简单的给出DAAB的使用过程,在下篇的进阶篇中我会写使用DAAB执行存储过程以及参数的传递、事务的支持,连接信息的加密等内容。