ASP.NET 2.0數(shù)據(jù)教程:完成數(shù)據(jù)訪問層
第五步:完成數(shù)據(jù)訪問層
注意,ProductsTableAdapters類從Products表中返回的是CategoryID和SupplierID的值,但并不包括Categories表 的CategoryName字段和Suppliers表的CompanyName字段,盡管當(dāng)我們顯示產(chǎn)品信息時,這些很可能是我們想要顯示的字段。我們可以擴(kuò)充TableAdapter的起始方法GetProducts()來包含CategoryName和CompanyName字段的值,這方法進(jìn)而會更新強(qiáng)類型的DataTable來包括這些新的字段。
但這會造成一個問題,因為TableAdapter的插入,更新,刪除數(shù)據(jù)的方法是基于這個起始方法的,幸運的是,自動生成的插入,更新,刪除方法并不會受SELECT子句中的子查詢的影響。如果我們注意把對Categories和Suppliers的查詢添加成子查詢,而不是用JOIN語 句的話,我們可以避免重做這些修改數(shù)據(jù)的方法。在ProductsTableAdapter中的GetProducts()方法上按右鼠標(biāo),選擇“配置”,然后,把SELECT子句改成:
SQL
- SELECT ProductID, ProductName, SupplierID, CategoryID,
- QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued,
- (SELECT CategoryName FROM Categories
- WHERE Categories.CategoryID = Products.CategoryID) as CategoryName,
- (SELECT CompanyName FROM Suppliers
- WHERE Suppliers.SupplierID = Products.SupplierID) as SupplierName
- FROM Products
圖29: 更新GetProducts()方法的SELECT語句
在更新GetProducts()方法使用這個新查詢語句之后,對應(yīng)的DataTable將包含2個新字段,CategoryName和SupplierName。
圖30: Products DataTable多了2個新字段
花點時間把GetProductsByCategoryID(categoryID)方法中的SELECT 子句也更新一下。
如果你使用JOIN句法更新GetProducts()中的SELECT語句的話 ,DataSet設(shè)計器不能使用DB直接模式自動生成插入,更新,以及刪除數(shù)據(jù)庫記錄的方法。你必須手工生成這 些方法,就象本教程早先時候我們對InsertProduct方法的做法一樣。此外,你必須手工提供InsertCommand,UpdateCommand和DeleteCommand屬性值,假如你想使用批更新模式的話。
完成數(shù)據(jù)訪問層:添加其他的TableAdapter
到目前為止,我們只討論了針對單個數(shù)據(jù)表的單個TableAdapter。但是,Northwind數(shù)據(jù)庫里含有我們需要在我們的web應(yīng)用中使用的幾個相關(guān)的表。一個強(qiáng)類型的DataSet可以包含多個相關(guān)的DataTable。因此,為了完成我們的DAL,我們需要為這些我們將來要用到的數(shù)據(jù)表添加相應(yīng)的DataTable。步驟如下,打開 DataSet設(shè)計 器,在設(shè)計器上按右鼠標(biāo),選擇“添加/TableAdapter”。這會生成一個新的DataTable和TableAdapter,然后我們早先討論過的配置向?qū)敢阃瓿膳渲谩?/P>
花上幾分鐘,創(chuàng)建對應(yīng)于下列查詢的TableAdapter及其方法。注意,ProductsTableAdapter的查詢中包含了用以獲取每個產(chǎn)品的分類和供應(yīng)商名字的子查詢。另外,如果你是隨著教程在做的話,你已經(jīng)添加過ProductsTableAdapter類的GetProducts()和GetProductsByCategoryID(categoryID)方法了。
- ProductsTableAdapter
- GetProducts:
- SELECT ProductID, ProductName, SupplierID, CategoryID,
- QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,
- ReorderLevel, Discontinued , (SELECT CategoryName FROM
- Categories WHERE Categories.CategoryID =
- Products.ProductID) as CategoryName, (SELECT CompanyName
- FROM Suppliers WHERE Suppliers.SupplierID =
- Products.SupplierID) as SupplierName
- FROM Products
- GetProductsByCategoryID:
- SELECT ProductID, ProductName, SupplierID, CategoryID,
- QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,
- ReorderLevel, Discontinued , (SELECT CategoryName FROM
- Categories WHERE Categories.CategoryID =
- Products.ProductID) as CategoryName,
- (SELECT CompanyName FROM Suppliers WHERE
- Suppliers.SupplierID = Products.SupplierID) as SupplierName
- FROM Products
- WHERE CategoryID = @CategoryID
- GetProductsBySupplierID
- SELECT ProductID, ProductName, SupplierID, CategoryID,
- QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,
- ReorderLevel, Discontinued ,
- (SELECT CategoryName FROM Categories WHERE
- Categories.CategoryID = Products.ProductID)
- as CategoryName, (SELECT CompanyName FROM Suppliers
- WHERE Suppliers.SupplierID = Products.SupplierID)
- as SupplierName
- FROM Products
- WHERE SupplierID = @SupplierID
- GetProductByProductID
- SELECT ProductID, ProductName, SupplierID, CategoryID,
- QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,
- ReorderLevel, Discontinued , (SELECT CategoryName
- FROM Categories WHERE Categories.CategoryID =
- Products.ProductID) as CategoryName,
- (SELECT CompanyName FROM Suppliers
- WHERE Suppliers.SupplierID = Products.SupplierID)
- as SupplierName
- FROM Products
- WHERE ProductID = @ProductID
- CategoriesTableAdapter
- GetCategories
- SELECT CategoryID, CategoryName, Description
- FROM Categories
- GetCategoryByCategoryID
- SELECT CategoryID, CategoryName, Description
- FROM Categories
- WHERE CategoryID = @CategoryID
- SuppliersTableAdapter
- GetSuppliers
- SELECT SupplierID, CompanyName, Address, City,
- Country, Phone
- FROM Suppliers
- GetSuppliersByCountry
- SELECT SupplierID, CompanyName, Address,
- City, Country, Phone
- FROM Suppliers
- WHERE Country = @Country
- GetSupplierBySupplierID
- SELECT SupplierID, CompanyName, Address,
- City, Country, Phone
- FROM Suppliers
- WHERE SupplierID = @SupplierID
- EmployeesTableAdapter
- GetEmployees
- SELECT EmployeeID, LastName, FirstName,
- Title, HireDate, ReportsTo, Country
- FROM Employees
- GetEmployeesByManager
- SELECT EmployeeID, LastName, FirstName,
- Title, HireDate, ReportsTo, Country
- FROM Employees
- WHERE ReportsTo = @ManagerID
- GetEmployeeByEmployeeID
- SELECT EmployeeID, LastName, FirstName,
- Title, HireDate, ReportsTo, Country
- FROM Employees
- WHERE EmployeeID = @EmployeeID
圖31:完成數(shù)據(jù)訪問層:添加了四個TableAdapter后的DataSet設(shè)計器
【編輯推薦】