這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)ASP.NET 2.0中怎么完成數(shù)據(jù)訪問層,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
專注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)漢中免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
注意,ProductsTableAdapters類從Products表中返回的是CategoryID和SupplierID的值,但并不包括Categories表 的CategoryName字段和Suppliers表的CompanyName字段,盡管當我們顯示產(chǎn)品信息時,這些很可能是我們想要顯示的字段。我們可以擴充TableAdapter的起始方法GetProducts()來包含CategoryName和CompanyName字段的值,這方法進而會更新強類型的DataTable來包括這些新的字段。
但這會造成一個問題,因為TableAdapter的插入,更新,刪除數(shù)據(jù)的方法是基于這個起始方法的,幸運的是,自動生成的插入,更新,刪除方法并不會受SELECT子句中的子查詢的影響。如果我們注意把對Categories和Suppliers的查詢添加成子查詢,而不是用JOIN語 句的話,我們可以避免重做這些修改數(shù)據(jù)的方法。在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設(shè)計器不能使用DB直接模式自動生成插入,更新,以及刪除數(shù)據(jù)庫記錄的方法。你必須手工生成這 些方法,就象本教程早先時候我們對InsertProduct方法的做法一樣。此外,你必須手工提供InsertCommand,UpdateCommand和DeleteCommand屬性值,假如你想使用批更新模式的話。
完成數(shù)據(jù)訪問層:添加其他的TableAdapter
到目前為止,我們只討論了針對單個數(shù)據(jù)表的單個TableAdapter。但是,Northwind數(shù)據(jù)庫里含有我們需要在我們的web應用中使用的幾個相關(guān)的表。一個強類型的DataSet可以包含多個相關(guān)的DataTable。因此,為了完成我們的DAL,我們需要為這些我們將來要用到的數(shù)據(jù)表添加相應的DataTable。步驟如下,打開 DataSet設(shè)計 器,在設(shè)計器上按右鼠標,選擇“添加/TableAdapter”。這會生成一個新的DataTable和TableAdapter,然后我們早先討論過的配置向?qū)敢阃瓿膳渲谩?/p>
花上幾分鐘,創(chuàng)建對應于下列查詢的TableAdapter及其方法。注意,ProductsTableAdapter的查詢中包含了用以獲取每個產(chǎn)品的分類和供應商名字的子查詢。另外,如果你是隨著教程在做的話,你已經(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è)計器
上述就是小編為大家分享的ASP.NET 2.0中怎么完成數(shù)據(jù)訪問層了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。