扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
图一:BLL将表示层与DAL隔开了,并且加入了业务规则 |
图二:在BLL文件夹中添加4个新的类 |
1using System; 2using System.Data; 3using System.Configuration; 4using System.Web; 5using System.Web.Security; 6using System.Web.UI; 7using System.Web.UI.WebControls; 8using System.Web.UI.WebControls.WebParts; 9using System.Web.UI.HtmlControls; 10using NorthwindTableAdapters; 11 12[System.ComponentModel.DataObject] 13public class ProductsBLL 14{ 15 private ProductsTableAdapter _productsAdapter = null; 16 protected ProductsTableAdapter Adapter 17 { 18 get { 19 if (_productsAdapter == null) 20 _productsAdapter = new ProductsTableAdapter(); 21 22 return _productsAdapter; 23 } 24 } 25 26 27[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)] 28 public Northwind.ProductsDataTable GetProducts() 29 { 30 return Adapter.GetProducts(); 31 } 32 33 [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)] 34 public Northwind.ProductsDataTable GetProductByProductID(int productID) 35 { 36 return Adapter.GetProductByProductID(productID); 37 } 38 39[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)] 40 public Northwind.ProductsDataTable GetProductsByCategoryID(int categoryID) 41 { 42 return Adapter.GetProductsByCategoryID(categoryID); 43 } 44 45[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)] 46 public Northwind.ProductsDataTable GetProductsBySupplierID(int supplierID) 47 { 48 return Adapter.GetProductsBySupplierID(supplierID); 49 } 50 [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)] 51 public bool AddProduct(string productName, int? supplierID, int? categoryID, string quantityPerUnit, 52 decimal? unitPrice, short? unitsInStock, short? unitsOnOrder, short? reorderLevel, 53 bool discontinued) 54 { 55 // 新建一个ProductRow实例 56 Northwind.ProductsDataTable products = new Northwind.ProductsDataTable(); 57 Northwind.ProductsRow product = products.NewProductsRow(); 58 59 product.ProductName = productName; 60 if (supplierID == null) product.SetSupplierIDNull(); else product.SupplierID = supplierID.Value; 61 if (categoryID == null) product.SetCategoryIDNull(); else product.CategoryID = categoryID.Value; 62 if (quantityPerUnit == null) product.SetQuantityPerUnitNull(); else product.QuantityPerUnit = quantityPerUnit; 63 if (unitPrice == null) product.SetUnitPriceNull(); else product.UnitPrice = unitPrice.Value; 64 if (unitsInStock == null) product.SetUnitsInStockNull(); else product.UnitsInStock = unitsInStock.Value; 65 if (unitsOnOrder == null) product.SetUnitsOnOrderNull(); else product.UnitsOnOrder = unitsOnOrder.Value; 66 if (reorderLevel == null) product.SetReorderLevelNull(); else product.ReorderLevel = reorderLevel.Value; 67 product.Discontinued = discontinued; 68 69 // 添加新产品 70 products.AddProductsRow(product); 71 int rowsAffected = Adapter.Update(products); 72 73 // 如果刚好新增了一条记录,则返回true,否则返回false 74 return rowsAffected == 1; 75 } 76 77 [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)] 78 public bool UpdateProduct(string productName, int? supplierID, int? categoryID, string quantityPerUnit, 79 decimal? unitPrice, short? unitsInStock, short? unitsOnOrder, short? reorderLevel, 80 bool discontinued, int productID) 81 { 82 Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID); 83 if (products.Count == 0) 84 // 没有找到匹配的记录,返回false 85 return false; 86 87 Northwind.ProductsRow product = products[0]; 88 89 product.ProductName = productName; 90 if (supplierID == null) product.SetSupplierIDNull(); else product.SupplierID = supplierID.Value; 91 if (categoryID == null) product.SetCategoryIDNull(); else product.CategoryID = categoryID.Value; 92 if (quantityPerUnit == null) product.SetQuantityPerUnitNull(); else product.QuantityPerUnit = quantityPerUnit; 93 if (unitPrice == null) product.SetUnitPriceNull(); else product.UnitPrice = unitPrice.Value; 94 if (unitsInStock == null) product.SetUnitsInStockNull(); else product.UnitsInStock = unitsInStock.Value; 95 if (unitsOnOrder == null) product.SetUnitsOnOrderNull(); else product.UnitsOnOrder = unitsOnOrder.Value; 96 if (reorderLevel == null) product.SetReorderLevelNull(); else product.ReorderLevel = reorderLevel.Value; 97 product.Discontinued = discontinued; 98 99 // 更新产品记录 100 int rowsAffected = Adapter.Update(product); 101 102 // 如果刚好更新了一条记录,则返回true,否则返回false 103 return rowsAffected == 1; 104 } 105 106 [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)] 107 public bool DeleteProduct(int productID) 108 { 109 int rowsAffected = Adapter.Delete(productID); 110 111 // 如果刚好删除了一条记录,则返回true,否则返回false 112 return rowsAffected == 1; 113 } 114} 115 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者