简化实现通用数据访问功能的开发任务。应用程序可以在很多情况下使用应用程序块,例如读取显示数据、获得通过应用程序层的数据,以及将更改过的数据提交回数据库系统等。应用程序块包括对存储过程和内嵌 SQL 以及常见内务处理任务(例如,管理连接、创建与缓存封装在应用程序块的方法中的参数)的支持。换句话说,数据访问应用程序块提供对最常用的 ADO.NET 功能的访问。
1. 解决的问题:
(1) 它可以减少编写样本代码以执行标准任务的需要。
(2) 它有助于在应用程序和整个企业中维护一致的数据访问做法。
(3) 它可以降低更改物理数据库目标的难度。
(4) 它使开发人员免于学习不同类型数据库的不同编程模型。
(5) 将应用程序移植到不同类型的数据库时,它可以减少需要重新编写的代码数量。
2. 使用:
(1) 使用Enterprise Library Configuration打开应用程序配置文件。
(2) 添加引用:Microsoft.Practices.EnterpriseLibrary.Data.dll
using Microsoft.Practices.EnterpriseLibrary.Data;
(3) 创建数据库连接操作类:
Database db
=
DatabaseFactory.CreateDatabase(
"
NorthWind
"
);
(4) 执行SQL语句:
DataSet ds
=
db.ExecuteDataSet(CommandType.Text,commandText);
DataReader dr
=
db.ExecuteReader(CommandType.Text,commandText); 用完后记得要dr.Close();
object
result
=
db.ExecuteScalar(CommandType.Text,commandText);
//
或者
DbCommand dbCommand
=
db.GetSqlStringCommand(commandText);
object
result
=
db.Execute
*
(dbCommand);
(5) 执行存储过程:
DbCommand dbCommand
=
db.GetStoredProcCommand(storeProcName);
db.AddInParameter(dbCommand,
"
inParam
"
, DbType.Int32, Category);
//
注意这里和下面不用加@,DAAB会自动加。
db.AddOutParameter(dbCommand,
"
outParam
"
, DbType.Int32, Category);
object
result
=
db.Execute
*
(dbCommand);
object
outValue
=
db.GetParameterValue(dbCommand,
"
outParam
"
);
(6) 更新DataSet:
DbCommand insertCommand
=
db.GetStoredProcCommand(
"
AddCommand
"
);
//
可以再添加处理参数,下同
DbCommand deleteCommand
=
db.GetStoredProcCommand(
"
DeleteCommand
"
);
DbCommand updateCommand
=
db.GetStoredProcCommand(
"
UpdateCommand
"
);
int
rowsAffected
=
db.UpdateDataSet(dataSet,
"
tableName
"
, insertCommand, updateCommand,
deleteCommand, UpdateBehavior.Standard);
(7) 处理事务:
public
bool
Transfer(
int
transactionAmount,
int
sourceAccount,
int
destinationAccount)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
bool result = false;
Database db = DatabaseFactory.CreateDatabase();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string sqlCommand = "Command1";
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sqlCommand = "Comand2";
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
using (DbConnection connection = db.CreateConnection())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction();
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
db.ExecuteNonQuery(creditCommand, transaction);
db.ExecuteNonQuery(debitCommand, transaction);
transaction.Commit();
result = true;
}
catch
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
transaction.Rollback();
}
connection.Close();
return result;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3. Data Access Application Block 的设计:
![](https://i-blog.csdnimg.cn/blog_migrate/07a902e559e26cedea316397b81e1add.gif)
4. 暂时还没有搞明白的地方:
DbCommand dbCommand
=
db.GetStoredProcCommand(commandTest
/
storeProcName, productID);
object
value
=
db.ExecuteScalar(dbCommand);
Passing the productID value to the commandTest/storeProcName results in parameter discovery being used to correctly establish the parameter information for the productID. Subsequent calls to this method will cause the block to retrieve the parameter information from the cache, and not require rediscovery。
http://davidhayden.com/blog/dave/archive/2006/11/03/CachingStoredProcedureParameters.aspx
http://davidhayden.com/blog/dave/archive/2006/11/01/SqlCommandBuilderDeriveParameters.aspx
貌似是在说对参数进行缓存什么的,但不知道究竟有啥用?真的能提高性能?-_-