Enterprise Library Step By Step系列(四):数据访问程序块——进阶篇
写在前面:在进阶篇中,主要通过对使用存储过程,使用事务,加密连接串三部分内容的介绍,给企业库的初学者提供一点帮助。
一. 使用存储过程:
关于数据访问应用程序块的介绍在入门篇里面已经说过了,这里不再重复。下面我们来看看在数据访问应用程序块里面使用存储过程的方法:
1. 不传递参数:不传递参数时值需要指定存储过程的名称,已经命令的类型即可。
1
/// <summary>
2
/// 执行存储过程(1)——不传递参数(NoPara)
3
/// </summary>
4
private void FormatDataBySp1()
5
{
6
///创建数据库实例
7
Database db = DatabaseFactory.CreateDatabase("NorthWind");
8
9
///传递参数
10
DBCommandWrapper dbc = db.GetStoredProcCommandWrapper("GetProductsList");
11![](/Images/OutliningIndicators/InBlock.gif)
12
///执行存储过程
13
DataSet ds = db.ExecuteDataSet(dbc);
14
15
///绑定数据
16
DataGrid1.DataSource = ds.Tables[0];
17
DataGrid1.DataBind();
18
}
![](/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/ExpandedSubBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
2. 传递输入参数:需要注意的是在增加参数时是AddInParameter。
1
/// <summary>
2
/// 执行存储过程(2)——传递输入参数(Input)
3
/// </summary>
4
private void FormatDataBySp2()
5
{
6
///创建数据库实例
7
Database db = DatabaseFactory.CreateDatabase("NorthWind");
8
9
///传递参数
10
DBCommandWrapper dbc = db.GetStoredProcCommandWrapper("CustOrdersDetail");
11
dbc.AddInParameter("@OrderID",System.Data.DbType.Int32,10248);
12![](/Images/OutliningIndicators/InBlock.gif)
13
///执行存储过程
14
DataSet ds = db.ExecuteDataSet(dbc);
15
16
///绑定数据
17
DataGrid1.DataSource = ds.Tables[0];
18
DataGrid1.DataBind();
19
}
![](/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/ExpandedSubBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
3. 传递输出参数:需要注意的是在增加参数时是AddOutParameter。
1
/// <summary>
2
/// 执行存储过程(3)——传递输出参数(Output)
3
/// </summary>
4
private void FormatDataBySp3()
5
{
6
///创建数据库实例
7
Database db = DatabaseFactory.CreateDatabase("NorthWind");
8
9
///传递参数
10
DBCommandWrapper dbc = db.GetStoredProcCommandWrapper("GetProductDetails");
11![](/Images/OutliningIndicators/InBlock.gif)
12
dbc.AddInParameter("@ProductID",System.Data.DbType.Int32,12);
13
dbc.AddOutParameter("@ProductName",System.Data.DbType.String,50); //输出
14![](/Images/OutliningIndicators/InBlock.gif)
15
///执行存储过程
16
db.ExecuteNonQuery(dbc);
17
18
string result = dbc.GetParameterValue("@ProductName").ToString(); //得到输出参数的值
19
}
![](/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/ExpandedSubBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
二. 使用事务:
在企业库数据访问应用程序块里面,对事务提供了很好的支持,具体的使用方法在程序里面有注释,这里就不多说了。
1
/// <summary>
2
/// 利用事务更改数据
3
/// </summary>
4
private void ExecuteUseTran()
5
{
6
///创建数据库实例
7
Database db = DatabaseFactory.CreateDatabase("NorthWind");
8![](/Images/OutliningIndicators/InBlock.gif)
9
using(IDbConnection Idbconn = db.GetConnection())
10
{
11
///打开连接
12
Idbconn.Open();
13
14
///创建事务
15
IDbTransaction Idbtran = Idbconn.BeginTransaction();
16![](/Images/OutliningIndicators/InBlock.gif)
17
try
18
{
19
///执行两个存储过程
20
db.ExecuteNonQuery(CommandType.StoredProcedure,"InserOrders");
21
db.ExecuteDataSet(CommandType.StoredProcedure,"UpdateProducts");
22
23
///执行完成后提交事务
24
Idbtran.Commit();
25
}
26
catch
27
{
28
///回滚事务
29
Idbtran.Rollback();
30
}
31
finally
32
{
33
///关闭连接
34
Idbconn.Close();
35
}
36
}
37![](/Images/OutliningIndicators/InBlock.gif)
38
}
![](/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/ExpandedSubBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
三. 加密连接字符串:
1. 创建一个加密的Stored Provider:
2. 创建新的加密算法:
3. 选择加密算法:
4. 创建加密密钥:
5. 保存加密密钥的保存文件,该文件要小心保存:
6. 指定加密的部分:
这样我们看到,在配置文件里面的内容变成了乱码。
总结:关于数据访问应用程序块的进阶篇就写到这里了,希望对初学企业库的朋友有所帮助