ASP.NET数据库访问系列教程01-概述篇 创建数据访问层(下)
ASP.NET數(shù)據(jù)庫訪問系列教程
本教程深入探討了基于ASP.NET 2.0技術(shù)的數(shù)據(jù)庫訪問方法和模式。這些介紹非常簡明,并且提供了一步步的指導(dǎo)和大量的截屏。
該系列教程包括:
- 概述篇
- 基礎(chǔ)報表
- 主/明細(xì)報表
- 自定義格式報表
- 編輯,插入和刪除數(shù)據(jù)
- 分頁和排序、自定義按鈕事件
- 使用DataList和Repeater控件顯示數(shù)據(jù)
- 使用DataList和Repeater進(jìn)行數(shù)據(jù)篩選
- 通過DataList編輯和刪除數(shù)據(jù)
- 使用DataList和Repeater控件進(jìn)行分頁和排序
- 自定義DataList和Repeater控件的按鈕事件
- 從ASP.NET頁面直接訪問數(shù)據(jù)庫
- 擴展GridView控件
- 操作二進(jìn)制文件
- 數(shù)據(jù)緩存
- 基于數(shù)據(jù)庫的站點地圖
- 批量數(shù)據(jù)處理
- 高級數(shù)據(jù)庫訪問操作
#1 概述篇-創(chuàng)建數(shù)據(jù)訪問層(下)
本文檔是 Visual C# 教程該教程從頭開始使用 Typed DataSet(強類型 DataSet)創(chuàng)建數(shù)據(jù)訪問層 (DAL),以訪問數(shù)據(jù)庫中的信息。
步驟 5:完成數(shù)據(jù)訪問層
注意,ProductsTableAdapters 類返回Products 表的 CategoryID 和 SupplierID 值,但不包含Categories 表的 CategoryName 列,或 Suppliers 表的 CompanyName 列,盡管在顯示產(chǎn)品信息時我們可能也希望顯示這些列。我們可以擴充TableAdapter 初始方法GetProducts() ,使其包括 CategoryName 和 CompanyName 列值,這將更新強類型的DataTable ,使其同樣包含這些新列。
但是,這樣就會出現(xiàn)一個問題,因為TableAdapter 的添加、更新和刪除方法并不是基于這個初始方法。幸運的是,自動生成的添加、更新和刪除方法不受SELECT 子句中的子查詢影響。通過把對 Categories 和Suppliers 的查詢作為子查詢添加到我們原來的查詢語句中,而不是使用JOIN 連接,我們可以避免重寫這些用來修改數(shù)據(jù)的方法。右鍵單擊ProductsTableAdapter 中的 GetProducts() 方法并選擇Configure 。隨后將 SELECT 子句修改如下:
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 將包含下面兩個新列:CategoryName 和SupplierName 。
圖30 :Products 數(shù)據(jù)表有兩個新列
?
花點時間也來更新 GetProductsByCategoryID(categoryID) 方法中的 SELECT 子句。
如果使用 JOIN 語法更新 GetProducts() 中的 SELECT ,DataSet 設(shè)計器將不能使用數(shù)據(jù)庫直接模式自動生成數(shù)據(jù)插入、更新和刪除的方法。您不得不手動的生成這些方法,就好象在本教程早先時候我們對InsertProduct 方法的做法一樣。另外,如果您希望使用批量更新模式,就必須手動提供InsertCommand 、UpdateCommand 和DeleteCommand 屬性值。
?
添加剩余的TableAdapters
至今為止,我們只是介紹了單個數(shù)據(jù)庫表的單個TableAdapter 。但是,Northwind 數(shù)據(jù)庫包含需要我們在我們的 Web 應(yīng)用程序中操作的幾個相關(guān)表。一個 Typed DataSet 可以包含多個相關(guān)的 DataTable 。因此,要完成我們的 DAL ,還需要為我們在這些教程中用到的其它表添加DataTable 。要添加新的 TableAdapter 到 Typed DataSet ,打開 DataSet Designer ,右鍵單擊Designer 并選擇 Add / TableAdapter 。這將創(chuàng)建一個新的 DataTable 和 TableAdapter ,并引導(dǎo)你完成我們在前面教程所討論的配置向?qū)А?/p>
花幾分鐘的時間,用下面的查詢語句創(chuàng)建對應(yīng)的TableAdapters 及其方法。注意,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.CategoryID) 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.CategoryID) 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.CategoryID) 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.CategoryID) as CategoryName,
(SELECT CompanyName FROM Suppliers WHERE Suppliers.SupplierID = Products.SupplierID)
as SupplierName
FROM Products
WHERE ProductID = @ProductID
- GetProducts:
- CategoriesTableAdapter
- GetCategories:
SELECT CategoryID, CategoryName, Description
FROM Categories - GetCategoryByCategoryID:
SELECT CategoryID, CategoryName, Description
FROM Categories
WHERE CategoryID = @CategoryID
- GetCategories:
- 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
- GetSuppliers:
- 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
- GetEmployees:
圖31 :添加了四個TableAdapter 的 DataSet 設(shè)計器
?
向 DAL 添加自定義代碼
添加到 Typed DataSet 的TableAdapter 和 DataTable 由 XML Schema Definition 文件 (Northwind.xsd) 來描述。通過右鍵單擊 Solution Explorer 中的Northwind.xsd 文件并選擇View Code ,可以查看該 schema 的信息。
圖32 :針對Northwinds Typed DataSet 的 XML Schema Definition (XSD) 文件
編譯或運行時(如果需要),該schema 信息在設(shè)計時被譯成 C# 或 Visual Basic 代碼,此時您可以使用調(diào)試器進(jìn)行調(diào)試。要查看這個自動生成的代碼,轉(zhuǎn)入Class View 并找到TableAdapter 或 Typed DataSet 類。如果在屏幕上看不到 Class View ,轉(zhuǎn)入View 菜單并選中它,或按下 Ctrl+Shift+C 。從 Class View 上可以看到 Typed DataSet 和TableAdapter 類的屬性、方法和事件。要查看某個方法的代碼,雙擊Class View 中該方法的名稱或右鍵單擊它并選擇 Go To Definition 。
圖33 : 通過選擇Class View 的 Selecting Go To Definition 檢查自動生成的代碼
盡管自動生成的代碼可以節(jié)省很多時間,但是它們通常都是通用代碼,需要自定義來滿足應(yīng)用程序的特定要求。可是,拓展自動生成代碼的風(fēng)險在于生成代碼的工具可以決定何時“再生成”而覆蓋了您的自定義操作。有了.NET 2.0 的新的部分類概念,我們可以非常簡單的將一個類的定義分寫在幾個文件中。這樣我們能夠添加自己的方法、屬性和事件到自動生成的類,而不必?fù)?dān)心Visual Studio 覆蓋了我們的自定義內(nèi)容。
為了說明如何自定義 DAL ,我們現(xiàn)在把GetProducts() 方法添加到SuppliersRow 類。SuppliersRow 類在Suppliers 表呈現(xiàn)一條記錄;每個供應(yīng)商可以提供零到多個產(chǎn)品,這樣GetProducts() 將返回指定供應(yīng)商的那些產(chǎn)品信息。要做到這些,需要在App_Code 文件夾中創(chuàng)建一個名為 SuppliersRow.cs 的新的類文件,然后在其中添加下列代碼:
2 using System.Data;
3 using NorthwindTableAdapters;
4
5 public partial class Northwind
6 {
7 public partial class SuppliersRow
8 {
9 public Northwind.ProductsDataTable GetProducts()
10 {
11 ProductsTableAdapter productsAdapter =
12 new ProductsTableAdapter();
13 return
14 productsAdapter.GetProductsBySupplierID(this.SupplierID);
15 }
16 }
17 }
該部分類指示編譯器創(chuàng)建 Northwind.SuppliersRow 類時包含我們剛定義的 GetProducts() 方法。如果您 build 您的項目,然后返回到 Class View ,將看見GetProducts() 現(xiàn)在被列為Northwind.SuppliersRow 的方法。
圖34 G:GetProducts() 方法現(xiàn)在是Northwind.SuppliersRow 類的一部分
GetProducts() 方法現(xiàn)在可用來列舉某供應(yīng)商的全套產(chǎn)品,如下面的代碼所示:
2 new NorthwindTableAdapters.SuppliersTableAdapter();
3
4 // Get all of the suppliers
5 Northwind.SuppliersDataTable suppliers =
6 suppliersAdapter.GetSuppliers();
7
8 // Enumerate the suppliers
9 foreach (Northwind.SuppliersRow supplier in suppliers)
10 {
11 Response.Write("Supplier: " + supplier.CompanyName);
12 Response.Write("<ul>");
13
14 // List the products for this supplier
15 Northwind.ProductsDataTable products = supplier.GetProducts();
16 foreach (Northwind.ProductsRow product in products)
17 Response.Write("<li>" + product.ProductName + "</li>");
18
19 Response.Write("</ul><p> </p>");
20 }
?
該數(shù)據(jù)也可以在任何 ASP.NET 的Web 數(shù)據(jù)控件中顯示。以下頁面使用了具有兩個字段的GridView 控件:
- 顯示每個供應(yīng)商名稱的 BoundField 和
- 一個 TemplateField ,它包含了一個 BulletedList 控件,該控件與每個供應(yīng)商的 GetProducts() 方法的返回結(jié)果綁定。
我們會在后面的教程中探討如何顯示這種主/ 明細(xì)報表。就目前而言,該示例旨在說明添加到Northwind.SuppliersRow 類的自定義方法的使用。
SuppliersAndProducts.aspx
2 AutoEventWireup="true" Inherits="SuppliersAndProducts" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
5 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
7 <html xmlns="http://www.w3.org/1999/xhtml" >
8 <head runat="server">
9 <title>Untitled Page</title>
10 <link href="Styles.css" rel="stylesheet" type="text/css" />
11 </head>
12 <body>
13 <form id="form1" runat="server">
14 <div>
15 <h2>
16 Suppliers and Their Products</h2>
17 <p>
18 <asp:GridView ID="GridView1" runat="server"
19 AutoGenerateColumns="False"
20 CssClass="DataWebControlStyle">
21 <HeaderStyle CssClass="HeaderStyle" />
22 <AlternatingRowStyle CssClass="AlternatingRowStyle" />
23 <Columns>
24 <asp:BoundField DataField="CompanyName"
25 HeaderText="Supplier" />
26 <asp:TemplateField HeaderText="Products">
27 <ItemTemplate>
28 <asp:BulletedList ID="BulletedList1"
29 runat="server"
30 DataSource="<%# ((Northwind.SuppliersRow) ((System.Data.DataRowView) Container.DataItem).Row).GetProducts() %>"
31 DataTextField="ProductName">
32 </asp:BulletedList>
33 </ItemTemplate>
34 </asp:TemplateField>
35 </Columns>
36 </asp:GridView>
37 </p>
38
39 </div>
40 </form>
41 </body>
42 </html>
SuppliersAndProducts.aspx.cs
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using NorthwindTableAdapters;
12
13 public partial class SuppliersAndProducts : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 SuppliersTableAdapter suppliersAdapter = new
18 SuppliersTableAdapter();
19 GridView1.DataSource = suppliersAdapter.GetSuppliers();
20 GridView1.DataBind();
21 }
22 }
圖35 :供應(yīng)商的公司名列在左列,他們的產(chǎn)品在右列
小結(jié)
創(chuàng)建DAL 應(yīng)該是開發(fā)一個Web 應(yīng)用程序的第一步,這要在開始創(chuàng)建您的表示層之前進(jìn)行。通過Visual Studio ,創(chuàng)建一個基于Typed DataSet 的DAL 就成為一項不需要編寫一行代碼,在10 到15 分鐘內(nèi)就可以完成的任務(wù)。教程后面的內(nèi)容仍舊圍繞DAL 進(jìn)行。 下一教程 我們將定義幾個業(yè)務(wù)規(guī)則并看看如何在單個業(yè)務(wù)邏輯層中實現(xiàn)它們。
快樂編程!
?
轉(zhuǎn)載于:https://www.cnblogs.com/MingDe/archive/2011/06/13/2079629.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET数据库访问系列教程01-概述篇 创建数据访问层(下)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3D打印设计软件 FreeCAD 入门
- 下一篇: [asp.net mvc 奇淫巧技] 0