Enterprise Library :数据访问程序块学习1
一.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.在项目中选择 属性|生成事件|生成后事件命令行 输入如下内容:
![](/Images/OutliningIndicators/None.gif)
第二步 创建数据库实例:
创建数据库实例有两种方法,一是默认的数据库实例,一是用命名实例映射到配置文件。
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](/Images/OutliningIndicators/None.gif)
第三步 执行SQL语句:
DAAB可以执行静态的SQL语句,或者存储过程等。
相关代码如下:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
26
![](/Images/OutliningIndicators/None.gif)
27
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)