溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ASP.NET 2.0中怎么完成數(shù)據(jù)訪問層

發(fā)布時間:2021-07-15 15:10:12 來源:億速云 閱讀:151 作者:Leah 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)ASP.NET 2.0中怎么完成數(shù)據(jù)訪問層,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

注意,ProductsTableAdapters類從Products表中返回的是CategoryID和SupplierID的值,但并不包括Categories表 的CategoryName字段和Suppliers表的CompanyName字段,盡管當(dāng)我們顯示產(chǎn)品信息時,這些很可能是我們想要顯示的字段。我們可以擴(kuò)充TableAdapter的起始方法GetProducts()來包含CategoryName和CompanyName字段的值,這方法進(jìn)而會更新強類型的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

ASP.NET 2.0中怎么完成數(shù)據(jù)訪問層

圖29: 更新GetProducts()方法的SELECT語句

在更新GetProducts()方法使用這個新查詢語句之后,對應(yīng)的DataTable將包含2個新字段,CategoryName和SupplierName。

ASP.NET 2.0中怎么完成數(shù)據(jù)訪問層

圖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)的表。一個強類型的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

ASP.NET 2.0中怎么完成數(shù)據(jù)訪問層

圖31:完成數(shù)據(jù)訪問層:添加了四個TableAdapter后的DataSet設(shè)計器

上述就是小編為大家分享的ASP.NET 2.0中怎么完成數(shù)據(jù)訪問層了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI