扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:Reeezak 来源:博客园 2007年11月6日
关键字: Windows
1[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)] 2public bool UpdateSupplierAddress(int supplierID, string address, string city, string country) 3{ 4 Northwind.SuppliersDataTable suppliers = Adapter.GetSupplierBySupplierID(supplierID); 5 if (suppliers.Count == 0) 6 // 没有找到匹配的项,返回false 7 return false; 8 else 9 { 10 Northwind.SuppliersRow supplier = suppliers[0]; 11 12 if (address == null) supplier.SetAddressNull(); else supplier.Address = address; 13 if (city == null) supplier.SetCityNull(); else supplier.City = city; 14 if (country == null) supplier.SetCountryNull(); else supplier.Country = country; 15 16 // 更新供应商的关于地址的信息 17 int rowsAffected = Adapter.Update(supplier); 18 19 // 如果刚好更新了一条记录,则返回true,否则返回false 20 return rowsAffected == 1; 21 } 22} 23 |
1 ProductsTableAdapter productsAdapter = new ProductsTableAdapter(); 2 GridView1.DataSource = productsAdapter.GetProducts(); 3 GridView1.DataBind(); |
1 ProductsBLL productLogic = new ProductsBLL(); 2 GridView1.DataSource = productLogic.GetProducts(); 3 GridView1.DataBind(); |
图三:GridView中显示的产品列表 |
图四:DataColumn提供了基本的字段级验证 |
图五:在App_Code文件夹中添加新类 |
1public partial class Northwind 2{ 3 public partial class ProductsDataTable 4 { 5 public override void BeginInit() 6 { 7 this.ColumnChanging += ValidateColumn; 8 } 9 10 void ValidateColumn(object sender, DataColumnChangeEventArgs e) 11 { 12 if(e.Column.Equals(this.UnitPriceColumn)) 13 { 14 if(!Convert.IsDBNull(e.ProposedValue) && (decimal)e.ProposedValue < 0) 15 { 16 throw new ArgumentException("UnitPrice cannot be less than zero", "UnitPrice"); 17 } 18 } 19 else if (e.Column.Equals(this.UnitsInStockColumn) || 20 e.Column.Equals(this.UnitsOnOrderColumn) || 21 e.Column.Equals(this.ReorderLevelColumn)) 22 { 23 if (!Convert.IsDBNull(e.ProposedValue) && (short)e.ProposedValue < 0) 24 { 25 throw new ArgumentException(string.Format("{0} cannot be less than zero", e.Column.ColumnName), e.Column.ColumnName); 26 } 27 } 28 } 29 } 30} |
1public bool UpdateProduct(string productName, int? supplierID, int? categoryID, string quantityPerUnit, 2 decimal unitPrice, short? unitsInStock, short? unitsOnOrder, short? reorderLevel, 3 bool discontinued, int productID) 4{ 5 Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID); 6 if (products.Count == 0) 7 // 没有找到匹配项,返回false 8 return false; 9 10 Northwind.ProductsRow product = products[0]; 11 12 // 业务规则检查 – 不能停用某供应商所提供的唯一一个产品 13 if (discontinued) 14 { 15 // 获取我们从这个供应商处获得的所有产品 16 Northwind.ProductsDataTable productsBySupplier = Adapter.GetProductsBySupplierID(product.SupplierID); 17 18 if (productsBySupplier.Count == 1) 19 // 这是我们从这个供应商处获得的唯一一个产品 20 throw new ApplicationException("You cannot mark a product as discontinued if its the only product purchased from a supplier"); 21 } 22 23 product.ProductName = productName; 24 if (supplierID == null) product.SetSupplierIDNull(); else product.SupplierID = supplierID.Value; 25 if (categoryID == null) product.SetCategoryIDNull(); else product.CategoryID = categoryID.Value; 26 if (quantityPerUnit == null) product.SetQuantityPerUnitNull(); else product.QuantityPerUnit = quantityPerUnit; 27 if (unitPrice == null) product.SetUnitPriceNull(); else product.UnitPrice = unitPrice.Value; 28 if (unitsInStock == null) product.SetUnitsInStockNull(); else product.UnitsInStock = unitsInStock.Value; 29 if (unitsOnOrder == null) product.SetUnitsOnOrderNull(); else product.UnitsOnOrder = unitsOnOrder.Value; 30 if (reorderLevel == null) product.SetReorderLevelNull(); else product.ReorderLevel = reorderLevel.Value; 31 product.Discontinued = discontinued; 32 33 // 更新产品记录 34 int rowsAffected = Adapter.Update(product); 35 36 // 如果刚好更新了一条记录,则返回true,否则返回false 37 return rowsAffected == 1; 38} 39 |
1 ProductsBLL productLogic = new ProductsBLL(); 2 3 // 更新ProductID为1的产品信息 4 try 5 { 6 // 这个操作将会失败,因为我们试图使用一个小于0的UnitPrice 7 productLogic.UpdateProduct("Scott's Tea", 1, 1, null, -14m, 10, null, null, false, 1); 8 } 9 catch (ArgumentException ae) 10 { 11 Response.Write("There was a problem: " + ae.Message); 12 } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者