生活随笔
收集整理的這篇文章主要介紹了
EnterpriseLibrary数据访问(4)使用数据访问器接收数据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼下載: 敏捷學(xué)院技術(shù)資源庫(kù)
1.引用
view source print?
| 1 | // TODO: Use Enterprise Library Data Block |
| 3 | using Microsoft.Practices.EnterpriseLibrary.Data; |
| 5 | using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; |
2.創(chuàng)建訪問(wèn)器
view source print?
| 1 | // TODO: Create private fields for Data accessors |
| 3 | ????????private DataAccessor<Category> categoryAccessor; |
| 5 | ????????private DataAccessor<Product> productAccessor; |
3. 初始化訪問(wèn)器
view source print?
| 001 | private void InitializeAccessors() |
| 004 | ????????????Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>(); |
| 006 | ????????????// TODO: Create category accessor 使用存儲(chǔ)過(guò)程 |
| 008 | ????????categoryAccessor = db.CreateSprocAccessor("GetCategories", |
| 010 | ????????????????MapBuilder<Category>.MapAllProperties() |
| 012 | ????????????????.Map(p => p.Name).ToColumn("CategoryName") |
| 014 | ????????????????.Map(p => p.ID).ToColumn("CategoryID") |
| 016 | ????????????????.Build()); |
| 019 | ????????????// TODO: Create product accessor 使用sql語(yǔ)句 |
| 021 | ????????????productAccessor = |
| 023 | ????????????????db.CreateSqlStringAccessor( |
| 025 | ????????????????"SELECT ProductID,ProductName,UnitPrice,LastUpdate FROM Products WHERE? CategoryID=@CategoryID", |
| 027 | ????????????????new GetProductsByIdParameterMapper(db), |
| 029 | ????????????????MapBuilder<Product> |
| 031 | ????????????????.MapAllProperties() |
| 033 | ????????????????.Map(p => p.Name).ToColumn("ProductName") |
| 035 | ????????????????.Map(p => p.ID).ToColumn("ProductID") |
| 037 | ????????????????.Map(p => p.UnitPrice).WithFunc(ApplyTax) //對(duì)價(jià)格的單獨(dú)處理 |
| 039 | ????????????????.DoNotMap(p => p.LastReviewed)//不映射這個(gè)字段 |
| 043 | ????????????????.Build()); |
| 048 | ????????private decimal ApplyTax(IDataRecord record) |
| 052 | ????????????var unitPrice = (decimal)record["UnitPrice"]; |
| 054 | ????????????if (numTax.Value > 0) |
| 058 | ????????????????unitPrice += unitPrice * numTax.Value / 100; |
| 062 | ????????????return unitPrice; |
| 070 | ??public class GetProductsByIdParameterMapper : IParameterMapper |
| 074 | ????????private readonly Database db; |
| 076 | ????????public GetProductsByIdParameterMapper(Database db) |
| 080 | ????????????this.db = db; |
| 084 | ????????public void AssignParameters(System.Data.Common.DbCommand command, object[] parameterValues) |
| 088 | ????????????InitializeParameters(command); |
| 090 | ????????????db.SetParameterValue(command, "@CategoryID", parameterValues[0]); |
| 097 | ????????private void InitializeParameters(System.Data.Common.DbCommand command) |
| 101 | ????????????db.AddInParameter(command, "@CategoryID", System.Data.DbType.Int32); |
4. 執(zhí)行返回?cái)?shù)據(jù)
view source print?
| 01 | ?// TODO: Use a Data Accessor to retrieve Categories |
| 03 | ????????????var categories = categoryAccessor.Execute(); |
| 05 | ????????????cmbCategory.DataSource = categories.ToList(); |
| 07 | // TODO: Retrieve Products by Category |
| 09 | ????????????var selectedCategory = (Category)cmbCategory.SelectedItem; |
| 11 | ????????????if (selectedCategory == null) |
| 13 | ????????????????return; |
| 15 | ????????????var products = productAccessor.Execute(selectedCategory.ID); |
| 17 | ????????????dgProducts.DataSource = products.ToList(); |
附 簡(jiǎn)單總結(jié):如果數(shù)據(jù)庫(kù)中的字段和實(shí)體字段的名稱相同
view source print?
| 03 | using Microsoft.Practices.EnterpriseLibrary.Data; |
| 05 | using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; |
| 09 | private Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>(); |
| 11 | private DataAccessor<Product> productAccessor; |
| 15 | private List<Product> GetProducts() |
| 21 | ????????????productAccessor = |
| 23 | ??????????????db.CreateSqlStringAccessor( |
| 25 | ??????????????"SELECT ProductID as id,ProductName,UnitPrice,LastUpdate FROM Products", |
| 27 | ??????????????MapBuilder<Product> |
| 29 | ??????????????.MapAllProperties() |
| 31 | ??????????????//.Map(p=>p.ID).ToColumn("ProductID") |
| 33 | ??????????????.DoNotMap(p=>p.CategoryID) //不映射這個(gè)字段 |
| 41 | ????????????return productAccessor.Execute().ToList(); |
轉(zhuǎn)載于:https://www.cnblogs.com/xingquan/archive/2011/07/08/2100916.html
總結(jié)
以上是生活随笔為你收集整理的EnterpriseLibrary数据访问(4)使用数据访问器接收数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。