微软企业库5.0学习笔记(三十三)数据访问模块
前言
鑒于企業(yè)庫5.0已經(jīng)發(fā)布正式版,同時有廣大讀者的要求(臭屁一下,o(∩_∩)o...),后面文章的內(nèi)容和代碼將基于Enterprise Library5.0和Unity2.0來寫,感謝大家的一貫支持。
正文
數(shù)據(jù)庫訪問模塊都能實現(xiàn)哪些功能呢?數(shù)據(jù)庫訪問模塊抽象類你正在使用的數(shù)據(jù)庫,提供了一些列接口,使得你可以更容易的實現(xiàn)常用的數(shù)據(jù)庫訪問功能。例如:使用Database類填充DataSet數(shù)據(jù)集,用database類獲取一個適當(dāng)?shù)腃ommand實例,然后調(diào)用database的ExecuteDataSet方法,就可以填充數(shù)據(jù)集。不需要你調(diào)用DataAdapter的Fill方法。ExecuteDataSet方法管理數(shù)據(jù)庫連接,實現(xiàn)了填充數(shù)據(jù)集所需要的所有工作。使用類似的方法,通過database類可以直接執(zhí)行command,可以獲取一個DataReader實例,可以用dataset中的數(shù)據(jù)更新數(shù)據(jù)庫。模塊也支持多個操作的事務(wù),如果失敗的話,可以回滾。
除了使用ADO.NET也能完成的常用功能以外,模塊還支持異步訪問數(shù)據(jù)庫(只要數(shù)據(jù)支持)。還可以返回客戶端可以用LINQ查詢的數(shù)據(jù)的序列對象形式。
使用數(shù)據(jù)訪問模塊的主要優(yōu)點,除了簡化開發(fā)以外,它使得你可以創(chuàng)建一種provider獨立的應(yīng)用,可以很容易的更換不同的數(shù)據(jù)提供源。在大多數(shù)情況下,除非你在代碼中指定了數(shù)據(jù)庫類型,剩下的就是更改配置文件中的連接字符串配置節(jié)就可以了。不需要你修改代碼中的sql查詢和存儲過程及其參數(shù)。
數(shù)據(jù)訪問模塊提供的常用方法
下面列出一些模塊常用的獲取數(shù)據(jù)、更新數(shù)據(jù)方法,其中有一些和直接使用ADO.NET中的方法很相似。
?
| 方法 | 功能 |
| ExecuteDataset,創(chuàng)建,加載,返回數(shù)據(jù)集 LoadData,加載數(shù)據(jù)到一個已經(jīng)存在的數(shù)據(jù)集 UpdateDataSet,使用已經(jīng)存在的數(shù)據(jù)集更新數(shù)據(jù)庫內(nèi)容 | 填充一個數(shù)據(jù)集,使用數(shù)據(jù)集更新數(shù)據(jù)庫 |
| ExecuteReader,創(chuàng)建,返回一個provider獨立的DbDataReader實例 | 從數(shù)據(jù)庫讀取多行數(shù)據(jù) |
| ExecuteNonQuery,執(zhí)行command,返回數(shù)據(jù)庫受影響的行數(shù),可以通過output返回多個值 ExecuteScalar,執(zhí)行command,返回單個值 | 執(zhí)行command數(shù)據(jù)庫命令對象 |
| ExecuteSproAccessor,使用存儲過程返回一個客戶端可以查詢的序列對象 ExecuteSqlStringAccessor,使用SQL語句返回一個客戶端可以查詢的序列對象 | 以序列對象的形式返回數(shù)據(jù) |
| ExecuteXmlReader,返回xml格式的數(shù)據(jù),xmlReader類型,這個只能用在SQL Server數(shù)據(jù)庫,通過SqlDatabase類調(diào)用,Database類中沒有這個方法。 | 獲取xml格式數(shù)據(jù)(只能用在SQL Server數(shù)據(jù)庫) |
| GetStoredProcCommand,返回一個存儲過程的數(shù)據(jù)庫command對象 GetSqlStringCommand,返回一個SQL語句的數(shù)據(jù)庫command對象 | 創(chuàng)建一個Command對象 |
| AddInParameter,創(chuàng)建一個新的input參數(shù),并且加入command的參數(shù)集合 AddOutParameter,創(chuàng)建一個新的output參數(shù),并且加入command的參數(shù)集合 AddParameter,創(chuàng)建一個指定類型的參數(shù),并且加入command的參數(shù)集合 GetParameterValue,以Object類型返回指定參數(shù)的值 SetParameterValue,給指定參數(shù)賦值 ? | 處理command的參數(shù) |
| CreateConnection,創(chuàng)建,返回當(dāng)前數(shù)據(jù)庫的連接,允許你通過這個鏈接初始化和管理數(shù)據(jù)庫事務(wù) | 處理數(shù)據(jù)庫事務(wù) |
?
????? 如果你使用SqlDatabase類的話,可以使用Begin和End類型的方法實現(xiàn)數(shù)據(jù)庫異步操作。
????? 如何使用數(shù)據(jù)庫訪問模塊
????? 1首先通過企業(yè)庫的配置工具添加模塊配置
????? 2在代碼中添加如下代碼
?????
?
代碼 static?Database?defaultDB?=?null;static?Database?namedDB?=?null;
//?從容器中獲取默認的數(shù)據(jù)庫對象
//?The?actual?concrete?type?is?determined?by?the?configuration?settings.
defaultDB?=?EnterpriseLibraryContainer.Current.GetInstance<Database>();
//?從容器中獲取指定名稱的數(shù)據(jù)庫對象 namedDB
=?EnterpriseLibraryContainer.Current.GetInstance<Database>("ExampleDatabase");
?
?????? 如果你需要使用ExecuteXmlReader方法,或者是需要使用SQL Server數(shù)據(jù)庫對象的話,可以用下面的代碼。
?
代碼 static?SqlDatabase?sqlServerDB?=?null;//?Resolve?a?SqlDatabase?object?from?the?container?using?the?default?database.
sqlServerDB?=?EnterpriseLibraryContainer.Current.GetInstance<Database>()
as?SqlDatabase;
//?Assume?the?method?GetConnectionString?exists?in?your?application?and
//?returns?a?valid?connection?string.
string?myConnectionString?=?GetConnectionString();
SqlDatabase?sqlDatabase?=?new?SqlDatabase(myConnectionString);
?
????? 使用DataReader獲取多行數(shù)據(jù)
?
代碼 //?Call?the?ExecuteReader?method?by?specifying?just?the?stored?procedure?name.using?(IDataReader?reader?=?namedDB.ExecuteReader("MyStoredProcName"))
{
//?Use?the?values?in?the?rows?as?required.
}
//?Call?the?ExecuteReader?method?by?specifying?the?command?type
//?as?a?SQL?statement,?and?passing?in?the?SQL?statement.
using?(IDataReader?reader?=?namedDB.ExecuteReader(CommandType.Text,
"SELECT?TOP?1?*?FROM?OrderList"))
{
//?Use?the?values?in?the?rows?as?required?‐?here?we?are?just?displaying?them.
DisplayRowValues(reader);
}
?
????? 根據(jù)參數(shù)獲取多行數(shù)據(jù)
?????
?
代碼 using?(IDataReader?reader?=?defaultDB.ExecuteReader("ListOrdersByState",new?object[]?{?"Colorado"?}))
{
//?Use?the?values?in?the?rows?as?required?‐?here?we?are?just?displaying?them.
DisplayRowValues(reader);
}
?
?????
?????
????? 通過添加參數(shù)獲取多行數(shù)據(jù)
代碼 //?Read?data?with?a?SQL?statement?that?accepts?one?parameter.string?sqlStatement?=?"SELECT?TOP?1?*?FROM?OrderList?WHERE?State?LIKE?@state";
//?Create?a?suitable?command?type?and?add?the?required?parameter.
using?(DbCommand?sqlCmd?=?defaultDB.GetSqlStringCommand(sqlStatement))
{
defaultDB.AddInParameter(sqlCmd,?"state",?DbType.String,?"New?York");
//?Call?the?ExecuteReader?method?with?the?command.
using?(IDataReader?sqlReader?=?namedDB.ExecuteReader(sqlCmd))
{
Console.WriteLine("Results?from?executing?SQL?statement:");
DisplayRowValues(sqlReader);
}
}
?
?????
?
?
?
代碼 //?Create?a?suitable?command?type?and?add?the?required?parameter.using?(DbCommand?sprocCmd?=?defaultDB.GetStoredProcCommand(storedProcName))
{
defaultDB.AddInParameter(sprocCmd,?"state",?DbType.String,?"New?York");
//?Call?the?ExecuteReader?method?with?the?command.
using?(IDataReader?sprocReader?=?namedDB.ExecuteReader(sprocCmd))
{
Console.WriteLine("Results?from?executing?stored?procedure:");
DisplayRowValues(sprocReader);
}
}
?
????? 以對象形式返回數(shù)據(jù)
?????
????? 上圖是一個使用Accessor訪問數(shù)據(jù)庫,返回對象形式的數(shù)據(jù)的過程。
未完待續(xù)。。。。。。。。。。。。。。。。。。。。。。。。
?
轉(zhuǎn)載于:https://www.cnblogs.com/virusswb/archive/2010/05/14/Enterprise-Library-DataAccessBlock-3.html
總結(jié)
以上是生活随笔為你收集整理的微软企业库5.0学习笔记(三十三)数据访问模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android面试,BroadCastR
- 下一篇: 使用 Python 构建电影推荐系统