微软企业库5.0学习笔记(三十三)数据访问模块
前言
鑒于企業庫5.0已經發布正式版,同時有廣大讀者的要求(臭屁一下,o(∩_∩)o...),后面文章的內容和代碼將基于Enterprise Library5.0和Unity2.0來寫,感謝大家的一貫支持。
正文
數據庫訪問模塊都能實現哪些功能呢?數據庫訪問模塊抽象類你正在使用的數據庫,提供了一些列接口,使得你可以更容易的實現常用的數據庫訪問功能。例如:使用Database類填充DataSet數據集,用database類獲取一個適當的Command實例,然后調用database的ExecuteDataSet方法,就可以填充數據集。不需要你調用DataAdapter的Fill方法。ExecuteDataSet方法管理數據庫連接,實現了填充數據集所需要的所有工作。使用類似的方法,通過database類可以直接執行command,可以獲取一個DataReader實例,可以用dataset中的數據更新數據庫。模塊也支持多個操作的事務,如果失敗的話,可以回滾。
除了使用ADO.NET也能完成的常用功能以外,模塊還支持異步訪問數據庫(只要數據支持)。還可以返回客戶端可以用LINQ查詢的數據的序列對象形式。
使用數據訪問模塊的主要優點,除了簡化開發以外,它使得你可以創建一種provider獨立的應用,可以很容易的更換不同的數據提供源。在大多數情況下,除非你在代碼中指定了數據庫類型,剩下的就是更改配置文件中的連接字符串配置節就可以了。不需要你修改代碼中的sql查詢和存儲過程及其參數。
數據訪問模塊提供的常用方法
下面列出一些模塊常用的獲取數據、更新數據方法,其中有一些和直接使用ADO.NET中的方法很相似。
?
| 方法 | 功能 |
| ExecuteDataset,創建,加載,返回數據集 LoadData,加載數據到一個已經存在的數據集 UpdateDataSet,使用已經存在的數據集更新數據庫內容 | 填充一個數據集,使用數據集更新數據庫 |
| ExecuteReader,創建,返回一個provider獨立的DbDataReader實例 | 從數據庫讀取多行數據 |
| ExecuteNonQuery,執行command,返回數據庫受影響的行數,可以通過output返回多個值 ExecuteScalar,執行command,返回單個值 | 執行command數據庫命令對象 |
| ExecuteSproAccessor,使用存儲過程返回一個客戶端可以查詢的序列對象 ExecuteSqlStringAccessor,使用SQL語句返回一個客戶端可以查詢的序列對象 | 以序列對象的形式返回數據 |
| ExecuteXmlReader,返回xml格式的數據,xmlReader類型,這個只能用在SQL Server數據庫,通過SqlDatabase類調用,Database類中沒有這個方法。 | 獲取xml格式數據(只能用在SQL Server數據庫) |
| GetStoredProcCommand,返回一個存儲過程的數據庫command對象 GetSqlStringCommand,返回一個SQL語句的數據庫command對象 | 創建一個Command對象 |
| AddInParameter,創建一個新的input參數,并且加入command的參數集合 AddOutParameter,創建一個新的output參數,并且加入command的參數集合 AddParameter,創建一個指定類型的參數,并且加入command的參數集合 GetParameterValue,以Object類型返回指定參數的值 SetParameterValue,給指定參數賦值 ? | 處理command的參數 |
| CreateConnection,創建,返回當前數據庫的連接,允許你通過這個鏈接初始化和管理數據庫事務 | 處理數據庫事務 |
?
????? 如果你使用SqlDatabase類的話,可以使用Begin和End類型的方法實現數據庫異步操作。
????? 如何使用數據庫訪問模塊
????? 1首先通過企業庫的配置工具添加模塊配置
????? 2在代碼中添加如下代碼
?????
?
代碼 static?Database?defaultDB?=?null;static?Database?namedDB?=?null;
//?從容器中獲取默認的數據庫對象
//?The?actual?concrete?type?is?determined?by?the?configuration?settings.
defaultDB?=?EnterpriseLibraryContainer.Current.GetInstance<Database>();
//?從容器中獲取指定名稱的數據庫對象 namedDB
=?EnterpriseLibraryContainer.Current.GetInstance<Database>("ExampleDatabase");
?
?????? 如果你需要使用ExecuteXmlReader方法,或者是需要使用SQL Server數據庫對象的話,可以用下面的代碼。
?
代碼 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獲取多行數據
?
代碼 //?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);
}
?
????? 根據參數獲取多行數據
?????
?
代碼 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);
}
?
?????
?????
????? 通過添加參數獲取多行數據
代碼 //?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);
}
}
?
????? 以對象形式返回數據
?????
????? 上圖是一個使用Accessor訪問數據庫,返回對象形式的數據的過程。
未完待續。。。。。。。。。。。。。。。。。。。。。。。。
?
轉載于:https://www.cnblogs.com/virusswb/archive/2010/05/14/Enterprise-Library-DataAccessBlock-3.html
總結
以上是生活随笔為你收集整理的微软企业库5.0学习笔记(三十三)数据访问模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android面试,BroadCastR
- 下一篇: 使用 Python 构建电影推荐系统