Beginning ASP.NET 2.0
E-Commerce in C# 2005
2005入門 電子商務
第二章
現在比較流行的3層結構是表示層、業務層、數據層。
正確的邏輯纔有正確的結構。
我們的例子是氣球商店。也是使用3層結構。
我們的層次結構和使用到的技術。
表現層 使用asp.net Form Asp.net 用戶控件 |
業務層 C#類 |
數據層 Sqlserver |
Exercise:增加web項目
File ➤ New Web Site.
創建了一個默認的頁面 default.asp
按F5可以進行調試。
Exercise:建立第一個頁面
增加一個MasterPage項目。中文叫 母板頁。
在頁面中使用form和table建立頁面框架。
<%@ Master Language="C#" AutoEventWireup="true"
CodeFile="BalloonShop.master.cs" Inherits="BalloonShop" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>BalloonShop</title>
</head>
<body>
<form id="Form1" runat="server">
<table cellspacing="0" cellpadding="0" width="770" border="0">
<tr>
<td width="220" valign="top">
List of Departments
<br />
List of Categories
<br />
</td>
<td valign="top">
Header
<asp:ContentPlaceHolder ID="contentPlaceHolder" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
</form>
</body>
</html>
其中有服務器端控件 contentplaceholder有綠色的箭頭。
刪除默認頁。
新建一個默認aspx頁面,在創建的時候複選“選擇母板”。
這裏好像,這個頁面只能對母板頁中標記爲contentplaceholder的部分進行操作。
How It Works: The Main Web Page
它是怎樣工作的
Right now you have the skeleton of the first BalloonShop page in place. Perhaps it’s not apparent right now, butworking with Master Pages will save you a lot of headaches later on when you extend the site.
現在,你已經爲第一個頁面增加了骨架,現在還沒有準備好,不過使用母板將減輕以後的麻煩。
The Master Page establishes the layout of the pages that implement it, and these pages have the freedom to updatethe contents of the ContentPlaceHolder elements.
母板能夠規劃頁面。這些俄頁面也可以自由的更新內容,通過ContentPlaceHolder元素。
In our case, the header, the list of departments, and the listof categories are standard elements that will appear in every page of the web site (although the list of categories willhave blank output in some cases and will appear only when a department is selected—you’ll see more about thisin the next chapter).
在這個例子中,其他部分都是標準元素,每一頁都是一樣的。
注意 母板頁不僅僅只能包含ContentPlaceHolder元素。
創建站點圖片
創建用戶控件文件夾 UserControls
將寫好的用戶控件header.ascx插入到母板中。
與sqlservice 2005 聯機
使用存儲過程 它是通過業務層,屬於數據層的一部分。
Data ➤ Add New ➤ Stored Procedure.
完成後運行。右鍵存儲過程。
增加網站邏輯
業務層是應用程序的大腦,因爲它是負責程序業務邏輯。
對於這個業務我們需要建立3個類穩健。GenericDataAccess、CatalogAccess、BalloonShopConfigraion
對於sql2005 local使用(local)/SqlExpress
創建SqlCommand對象。使用存儲過程。
// Open the connection
conn.Open();
// Create the SqlDataReader object by executing the command
SqlDataReader reader = comm.ExecuteReader();
// Create a new DataTable and populate it from the SqlDataReader
DataTable table = new DataTable();
table.Load(reader);
// Close the reader and the connection
reader.Close();
conn.Close();
。net2.0新增了DbProviderFactory
對於發送email,當網站發生錯誤,我們可以使用發mail的方式發送報告。
SmtpClient and MailMessage classes
創建業務邏輯代碼。
將數據鏈接代碼寫在config文件。
<connectionStrings>
<add name="BalloonShopConnection" connectionString="Server=
(local)/SqlExpress;
Integrated Security=True;Database=BalloonShop"
providerName="System.Data.SqlClient"/>
</connectionStrings>
現在 網站的類文件要放在app_data下
public static DataTable ExecuteSelectCommand(DbCommand command)
{
// The DataTable to be returned 返回數據表
DataTable table;
// Execute the command making sure the connection gets closed in the end
try
{
// Open the data connection 打開數據鏈接
command.Connection.Open();
// Execute the command and save the results in a DataTable 執行後存入datatable
DbDataReader reader = command.ExecuteReader();
table = new DataTable();
table.Load(reader);
// Close the reader
reader.Close();
}
catch (Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
return table;
}
範例 通過command得到table
這個數據層由2個基礎數據工具類和1個業務邏輯類組成。
將得到的數據顯示出來。
注意 在webconfig中要加入
<identity impersonate="true" /> 在能得到認證通過
這裏用來填充的是datalist
增加一個自定義的錯誤頁。
先創建全局應用程序類。
void Application_Error(Object sender, EventArgs e)
{
// Log all unhandled errors
Utilities.LogError(Server.GetLastError());
}
在config.xml裏
<customErrors mode="RemoteOnly" defaultRedirect="Oooops.aspx" />