ASP. NET 2.0 data Tutorial: completes the data access layer

Source: Internet
Author: User

Step 5: complete the data access layer

Note that the ProductsTableAdapters class returns the values of CategoryID and SupplierID from the Products table, but does not include the CategoryName field of the Categories table and the CompanyName field of the Suppliers table, although when we display product information, these are probably the fields we want to display. We can expand the start method GetProducts () of TableAdapter to include the values of the CategoryName and CompanyName fields. This method will then update the strong-type able to include these new fields.

However, this may cause a problem because the TableAdapter insertion, update, and data deletion methods are based on this starting method. Fortunately, the automatically generated insert and update methods are used, the deletion method is not affected by the subquery in the select clause. If we add the queries for Categories and Suppliers into subqueries instead of using JOIN clauses, we can avoid redoing these data modification methods. Right-click the GetProducts () method in ProductsTableAdapter and SELECT "configuration". Then, change the SELECT clause:

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  

 

Figure 29: update the SELECT statement of the GetProducts () method

After the GetProducts () method is updated to use this new query statement, the corresponding DataTable will contain two new fields, CategoryName and SupplierName.

Figure 30: Products DataTable has two more fields

Take some time to update the SELECT clause in the GetProductsByCategoryID (categoryID) method.

If you use the JOIN syntax to update the SELECT statement in GetProducts (), the DataSet Designer cannot use the DB direct mode to automatically generate insert, update, and delete database records. You must manually generate these methods, just as we did with the InsertProduct method earlier in this tutorial. In addition, you must manually provide the InsertCommand, UpdateCommand, and DeleteCommand attribute values, if you want to use the batch update mode.

Complete data access layer: add other TableAdapter

So far, we have only discussed a single TableAdapter for a single data table. However, the Northwind database contains several related tables that we need to use in our web applications. A strongly-typed DataSet can contain multiple related datatables. Therefore, to complete our DAL, we need to add the corresponding able for the data tables we will use in the future. Follow these steps to open the DataSet Designer, right-click the designer, and select "Add/TableAdapter ". This will generate a new able and TableAdapter, And the Configuration Wizard we discussed earlier will guide you through the configuration.

It takes several minutes to create the TableAdapter corresponding to the following query and its method. Note that the query of ProductsTableAdapter contains a subquery used to obtain the category and supplier name of each product. In addition, if you are following the tutorial, you have added the GetProducts () and GetProductsByCategoryID (categoryID) Methods of the ProductsTableAdapter class.

 
 
  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  

 

Figure 31: data access layer completed: DataSet Designer added after four tableadapters

  1. How to deploy the asp.net mvc program in IIS6.0
  2. Use Winform to build the asp.net mvc Framework
  3. Programming idea of ASP. NET Session failure
  4. ASP. NET Session state storage
  5. Understand ASP. NET Web Application Models

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.