您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關ASP.NET 2.0中怎么完成數據訪問層,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
注意,ProductsTableAdapters類從Products表中返回的是CategoryID和SupplierID的值,但并不包括Categories表 的CategoryName字段和Suppliers表的CompanyName字段,盡管當我們顯示產品信息時,這些很可能是我們想要顯示的字段。我們可以擴充TableAdapter的起始方法GetProducts()來包含CategoryName和CompanyName字段的值,這方法進而會更新強類型的DataTable來包括這些新的字段。
但這會造成一個問題,因為TableAdapter的插入,更新,刪除數據的方法是基于這個起始方法的,幸運的是,自動生成的插入,更新,刪除方法并不會受SELECT子句中的子查詢的影響。如果我們注意把對Categories和Suppliers的查詢添加成子查詢,而不是用JOIN語 句的話,我們可以避免重做這些修改數據的方法。在ProductsTableAdapter中的GetProducts()方法上按右鼠標,選擇“配置”,然后,把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()方法使用這個新查詢語句之后,對應的DataTable將包含2個新字段,CategoryName和SupplierName。
圖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)方法了。
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:完成數據訪問層:添加了四個TableAdapter后的DataSet設計器
上述就是小編為大家分享的ASP.NET 2.0中怎么完成數據訪問層了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。