ASP.NET 2.0数据教程:完成数据访问层
创始人
2024-04-20 10:51:02
0

第五步:完成数据访问层

注意,ProductsTableAdapters类从Products表中返回的是CategoryID和SupplierID的值,但并不包括Categories表 的CategoryName字段和Suppliers表的CompanyName字段,尽管当我们显示产品信息时,这些很可能是我们想要显示的字段。我们可以扩充TableAdapter的起始方法GetProducts()来包含CategoryName和CompanyName字段的值,这方法进而会更新强类型的DataTable来包括这些新的字段。

但这会造成一个问题,因为TableAdapter的插入,更新,删除数据的方法是基于这个起始方法的,幸运的是,自动生成的插入,更新,删除方法并不会受SELECT子句中的子查询的影响。如果我们注意把对Categories和Suppliers的查询添加成子查询,而不是用JOIN语 句的话,我们可以避免重做这些修改数据的方法。在ProductsTableAdapter中的GetProducts()方法上按右鼠标,选择“配置”,然后,把SELECT子句改成:

SQL 

  1. SELECT     ProductID, ProductName, SupplierID, CategoryID,  
  2. QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued,  
  3. (SELECT CategoryName FROM Categories  
  4. WHERE Categories.CategoryID = Products.CategoryID) as CategoryName,  
  5. (SELECT CompanyName FROM Suppliers  
  6. WHERE Suppliers.SupplierID = Products.SupplierID) as SupplierName  
  7. FROM         Products  

更新GetProducts()方法的SELECT语句 

图29: 更新GetProducts()方法的SELECT语句

在更新GetProducts()方法使用这个新查询语句之后,对应的DataTable将包含2个新字段,CategoryName和SupplierName。

 Products DataTable多了2个新字段

图30: Products DataTable多了2个新字段

花点时间把GetProductsByCategoryID(categoryID)方法中的SELECT 子句也更新一下。

如果你使用JOIN句法更新GetProducts()中的SELECT语句的话 ,DataSet设计器不能使用DB直接模式自动生成插入,更新,以及删除数据库记录的方法。你必须手工生成这 些方法,就象本教程早先时候我们对InsertProduct方法的做法一样。此外,你必须手工提供InsertCommand,UpdateCommand和DeleteCommand属性值,假如你想使用批更新模式的话。

完成数据访问层:添加其他的TableAdapter

到目前为止,我们只讨论了针对单个数据表的单个TableAdapter。但是,Northwind数据库里含有我们需要在我们的web应用中使用的几个相关的表。一个强类型的DataSet可以包含多个相关的DataTable。因此,为了完成我们的DAL,我们需要为这些我们将来要用到的数据表添加相应的DataTable。步骤如下,打开 DataSet设计 器,在设计器上按右鼠标,选择“添加/TableAdapter”。这会生成一个新的DataTable和TableAdapter,然后我们早先讨论过的配置向导会指引你完成配置。

花上几分钟,创建对应于下列查询的TableAdapter及其方法。注意,ProductsTableAdapter的查询中包含了用以获取每个产品的分类和供应商名字的子查询。另外,如果你是随着教程在做的话,你已经添加过ProductsTableAdapter类的GetProducts()和GetProductsByCategoryID(categoryID)方法了。

  1. ProductsTableAdapter  
  2. GetProducts:  
  3. SELECT ProductID, ProductName, SupplierID, CategoryID,  
  4. QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,  
  5. ReorderLevel, Discontinued , (SELECT CategoryName FROM 
  6. Categories WHERE Categories.CategoryID =  
  7. Products.ProductID) as CategoryName, (SELECT CompanyName  
  8. FROM Suppliers WHERE Suppliers.SupplierID =  
  9. Products.SupplierID) as SupplierName  
  10. FROM Products  
  11. GetProductsByCategoryID:  
  12. SELECT ProductID, ProductName, SupplierID, CategoryID,  
  13. QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,  
  14. ReorderLevel, Discontinued , (SELECT CategoryName FROM 
  15. Categories WHERE Categories.CategoryID =  
  16. Products.ProductID) as CategoryName,  
  17. (SELECT CompanyName FROM Suppliers WHERE 
  18. Suppliers.SupplierID = Products.SupplierID) as SupplierName  
  19. FROM Products  
  20. WHERE CategoryID = @CategoryID  
  21. GetProductsBySupplierID  
  22. SELECT ProductID, ProductName, SupplierID, CategoryID,  
  23. QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,  
  24. ReorderLevel, Discontinued ,  
  25. (SELECT CategoryName FROM Categories WHERE 
  26. Categories.CategoryID = Products.ProductID)  
  27. as CategoryName, (SELECT CompanyName FROM Suppliers  
  28. WHERE Suppliers.SupplierID = Products.SupplierID)  
  29. as SupplierName  
  30. FROM Products  
  31. WHERE SupplierID = @SupplierID  
  32. GetProductByProductID  
  33. SELECT ProductID, ProductName, SupplierID, CategoryID,  
  34. QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,  
  35. ReorderLevel, Discontinued , (SELECT CategoryName  
  36. FROM Categories WHERE Categories.CategoryID =  
  37. Products.ProductID) as CategoryName,  
  38. (SELECT CompanyName FROM Suppliers  
  39. WHERE Suppliers.SupplierID = Products.SupplierID)  
  40. as SupplierName  
  41. FROM Products  
  42. WHERE ProductID = @ProductID  
  43.    
  44. CategoriesTableAdapter  
  45. GetCategories  
  46. SELECT CategoryID, CategoryName, Description  
  47. FROM Categories  
  48. GetCategoryByCategoryID  
  49. SELECT CategoryID, CategoryName, Description  
  50. FROM Categories  
  51. WHERE CategoryID = @CategoryID  
  52.    
  53. SuppliersTableAdapter  
  54. GetSuppliers  
  55. SELECT SupplierID, CompanyName, Address, City,  
  56. Country, Phone  
  57. FROM Suppliers  
  58. GetSuppliersByCountry  
  59. SELECT SupplierID, CompanyName, Address,  
  60. City, Country, Phone  
  61. FROM Suppliers  
  62. WHERE Country = @Country  
  63. GetSupplierBySupplierID  
  64. SELECT SupplierID, CompanyName, Address,  
  65. City, Country, Phone  
  66. FROM Suppliers  
  67. WHERE SupplierID = @SupplierID  
  68.    
  69. EmployeesTableAdapter  
  70. GetEmployees  
  71. SELECT EmployeeID, LastName, FirstName,  
  72. Title, HireDate, ReportsTo, Country  
  73. FROM Employees  
  74. GetEmployeesByManager  
  75. SELECT EmployeeID, LastName, FirstName,  
  76. Title, HireDate, ReportsTo, Country  
  77. FROM Employees  
  78. WHERE ReportsTo = @ManagerID  
  79. GetEmployeeByEmployeeID  
  80. SELECT EmployeeID, LastName, FirstName,  
  81. Title, HireDate, ReportsTo, Country  
  82. FROM Employees  
  83. WHERE EmployeeID = @EmployeeID  

添加了四个TableAdapter后的DataSet设计器 

图31:完成数据访问层:添加了四个TableAdapter后的DataSet设计器

【编辑推荐】

  1. 如何在IIS6.0中部署asp.net mvc程序
  2. 用Winform傻瓜式搭建asp.net mvc框架
  3. ASP.NET Session失效的编程思路
  4. ASP.NET Session 状态的存储
  5. 了解ASP.NET Web应用程序模型

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...