簡化實作通用資料通路功能的開發任務。應用程式可以在很多情況下使用應用程式塊,例如讀取顯示資料、獲得通過應用程式層的資料,以及将更改過的資料送出回資料庫系統等。應用程式塊包括對存儲過程和内嵌 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 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)
{
bool result = false;
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "Command1";
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);
sqlCommand = "Comand2";
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction();
try
{
db.ExecuteNonQuery(creditCommand, transaction);
db.ExecuteNonQuery(debitCommand, transaction);
transaction.Commit();
result = true;
}
catch
transaction.Rollback();
connection.Close();
return result;
}
}


3. Data Access Application Block 的設計:
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。
<a href="http://davidhayden.com/blog/dave/archive/2006/11/03/CachingStoredProcedureParameters.aspx">http://davidhayden.com/blog/dave/archive/2006/11/03/CachingStoredProcedureParameters.aspx</a>
<a href="http://davidhayden.com/blog/dave/archive/2006/11/01/SqlCommandBuilderDeriveParameters.aspx">http://davidhayden.com/blog/dave/archive/2006/11/01/SqlCommandBuilderDeriveParameters.aspx</a>
貌似是在說對參數進行緩存什麼的,但不知道究竟有啥用?真的能提高性能?-_-
本文轉自Silent Void部落格園部落格,原文連結:http://www.cnblogs.com/happyhippy/archive/2007/08/08/848294.html,如需轉載請自行聯系原作者