利用TableAdapter Configuration Wizard建立資料訪問層二

來源:互聯網
上載者:User
利用TableAdapter Configuration Wizard建立資料訪問層

在Visual Studio 2005 中,新增了TableAdapter configuration
wizard來很方便地建立資料訪問層。首先,我們瞭解下什麼是tableadapter。一個tableadapter串連到資料庫,執行查詢語句或者預存程序,並且將返回的結果集填充到datatable中去。Tableadapter
configuration 嚮導允許你以類型化dataset方式建立編輯資料集合,十分方便。

首先用C#語言,建立一個名為NtierExample的WEB項目,如:

為了建立資料訪問層,首先滑鼠右鍵點選工程項目菜單,在彈出的菜單中選擇"ADD New Item"。在彈出的"ADD NEW
ITEM"對話方塊中,選擇"dataset"類型。然後在檔案名稱中,輸入"Authors.xsd",並點擊"Add",如所示:

當你點"ADD"的按鈕時 ,系統會提示是否將該檔案放到APP_CODE目錄中去,因為VS.NET
2005中,一般會將資料訪問層的檔案放到該檔案夾中去,以方便管理。我們繼續選"ok",將xsd檔案放到app_code檔案夾中去。接下來,就出現
"TableAdpater"設定嚮導的視窗了。首先,我們要指定串連的資料庫字串,並選擇將連接字串儲存到web.config檔案中去,並選
"next"進入下一步。

在該步中,選擇命令類型,由於我們剛才建立了預存程序,因此選擇"Use existing store procedure",如:

再點選"NEXT",進入下一個視窗,會詢問採用哪一個預存程序,如:

這裡,我們選擇使用"GetAuthors"這個預存程序,再選"next"進入下一步,如:

在這裡,我們要指定使用GetAuthors預存程序的哪一個方法用來返回資料集,我們選擇"return a
datatable"的選現,並指定使用其中的GetAuthors方法,以datatable的形式返回。繼續選"next",出現如下視窗,則系統自
動會產生資料訪問層了。

當你點擊"Finish"按鈕後,Visual Studio 會自動產生一些類,當這些類產生後,我們將類改名為Authors,這樣,最後輸出如所示:

接下來,我們按上面的步驟,類似地,使用"TableAdapter"嚮導,選擇工具功能表列的"Data-Add-TableAdapter",再次
增加一個tableadapter,這次選擇的是"GetTitlesByAuthor"預存程序,而選擇返回的方法是
"GetTitlesByAuthor",其他步驟和產生"GetAuthos"的一樣,最後,將產生的類的名改為"AuthorTitles",如
所示

建立邏輯層

接下來,我們建立邏輯層,在這個例子中,邏輯層是十分簡單的,只是起到說明作用。首先,我們建立一個類AuthroBiz類,並將其放在App_code檔案夾中,並將類的代碼修改如下:

public class AuthorsBiz
{
public AuthorsBiz()
{}

public DataTable GetAuthors()
{
AuthorsTableAdapters.AuthorsTableAdapter authorDB = new
AuthorsTableAdapters.AuthorsTableAdapter();
return authorDB.GetAuthors();
}
public DataTable GetAuthorTitles(string authorID)
{
AuthorsTableAdapters.AuthorTitlesTableAdapter authorDB = new
AuthorsTableAdapters.AuthorTitlesTableAdapter();
return authorDB.GetTitlesByAuthor(authorID);
}
}

從上面的代碼中,可以看到,我們剛才通過嚮導建立的"Authors.xsd"類型化dataset類,現在在代碼中,可以通過使用AuthorsTableAdapters類來調用,其中authorDB是AuthorsTableAdapters類的執行個體。

  建立展示層

在ASP.NET
2.0中,在建立展示層時,可以使用master-page技術,使得可以很方便地構建頁面。Mater-page的意思是,可以首先構建出一個頁面的主
架構模版結構,然後在其中放置一個ContentPlaceHolder控制項,在該控制項中,將展現其他子頁面的內容。在其他子頁面中,只需要首先引用該
master頁面,然後再修改ContentPlaceHolder控制項的內容就可以了。

首先,在工程中新增加一個"master"類型的檔案,將其命名為CommonMaster,然後輸入以下代碼:

<%@ master language="C#" %>
<html>
<head id="Head1" runat="server">
<title>Master Page</title>
</head>
<body>
<form id="Form1" runat="server">
<table id="header" style="WIDTH: 100%; HEIGHT: 80px" cellspacing="1"
cellpadding="1" border="1">
<tr>
<td style="TEXT-ALIGN: center; width: 100%; height: 74px;" bgcolor="teal">
<asp:label runat="server" id="Header" Font-Size="12pt" Font-Bold="True">
Authors Information
</asp:label>
</td>
</tr>
</table>
<b/>
<table id="leftNav" style="WIDTH: 108px; HEIGHT: 100%" cellspacing="1"
cellpadding="1" border="1">
<tr>
<td style="WIDTH: 100px">
<table>
<tr>
<td>
<a href="Home.aspx">Home</a>
</td>
</tr>
<tr>
<td>
<a href="Authors.aspx">Authors List</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table id="mainBody" style="LEFT: 120px; VERTICAL-ALIGN: top; WIDTH: 848px;
POSITION: absolute; TOP: 94px; HEIGHT: 100%" border="1">
<tr>
<td width="100%" style="VERTICAL-ALIGN: top">
<asp:contentplaceholder id="middleContent" runat="Server"></asp:contentplaceholder>
</td>
</tr>
</table>
</form>
</body>
</html>

接下來,我們首先建立以顯示作者頁面的Authors.aspx頁面,由於頁面的架構要保持一直,因此,可以利用maser-page技術,在建立頁面時,引入剛才建立的CommonMaster頁面,如:

點ADD按鈕後,出現如,選擇剛才建立的CommonMaster頁面,如:

再輸入如下代碼:

<%@ Page Language="C#" MasterPageFile="~/CommonMaster.master" %>
<asp:content id="Content1" contentplaceholderid="middleContent" runat="server">
<asp:objectdatasource runat="server" id="authorsSource" typename="AuthorsBiz"
selectmethod="GetAuthors">
</asp:objectdatasource>
<asp:gridview runat="server" AutoGenerateColumns="false" id="authorsView"
datasourceid="authorsSource">
<alternatingrowstyle backcolor="Silver"></alternatingrowstyle>
<Columns>
<asp:HyperLinkField DataTextField="au_id" HeaderText="Author ID"
DataNavigateUrlFields="au_id"
DataNavigateUrlFormatString="AuthorTitles.aspx?AuthorID={0}">
</asp:HyperLinkField>
<asp:BoundField HeaderText="Last Name" DataField="au_lname"></asp:BoundField>
<asp:BoundField HeaderText="First Name" DataField="au_fname"></asp:BoundField>
<asp:BoundField HeaderText="Phone" DataField="phone"></asp:BoundField>
<asp:BoundField HeaderText="Address" DataField="address"></asp:BoundField>
<asp:BoundField HeaderText="City" DataField="city"></asp:BoundField>
<asp:BoundField HeaderText="State" DataField="state"></asp:BoundField>
<asp:BoundField HeaderText="Zip" DataField="zip"></asp:BoundField>

</Columns>
</asp:gridview>
</asp:content>

注意,其中我們用到了objectdatasource控制項,在.NET 2.0中,有了該控制項,可以很方便地溝通展示層和邏輯層。其中的代碼如下:

<asp:objectdatasource runat="server" id="authorsSource" typename="AuthorsBiz"
selectmethod="GetAuthors">
</asp:objectdatasource>

其中的typename屬性指定為我們之前建立的邏輯層的類AuthorsBiz類,而為了獲得資料,採用了selectmethod方法,這裡指定
了之前建立的GetAuthors方法。當然,也可以在其他場合,應用Updatemethod,Insertmethod,Deletemethod方
法,也可以加上參數,比如接下來要建立的AuthorTitle.aspx頁面,代碼如下:

<%@ Page Language="C#" MasterPageFile="~/CommonMaster.master" %>
<asp:content id="Content1" contentplaceholderid="middleContent" runat="server">
<asp:objectdatasource runat="server" id="authorTitlesSource" typename="AuthorsBiz"
selectmethod="GetAuthorTitles">
<SelectParameters>
<asp:QueryStringParameter Type="String" Direction="Input" Name="authorID"
QueryStringField="AuthorID" />
</SelectParameters>
</asp:objectdatasource>
<asp:gridview runat="server" id="authorTitlesView" datasourceid="authorTitlesSource">
<alternatingrowstyle backcolor="Silver"></alternatingrowstyle>
</asp:gridview>
</asp:content>

上面的代碼中,首先使用者在authors.aspx頁面點選某個作者名時,則在authortitle.aspx頁面中,返回該作者的所有著作。所
以,在objectdatasource控制項中,我們使用了SelectParameters參數,指定傳入來要查詢的參數是authorid。最後,再
將gridview綁定到objectdatasource控制項中去。

最後,運行我們的代碼,結果如下兩圖所表示:


小結

在ASP.NET 2.0中,我們利用SQL Server 2005的強大功能,可以利用.NET
語言建立預存程序,並使用TableAdapter嚮導,很方便地建立資料訪問層,再利用objectdatasource控制項的特性,可以很方便地溝通
展示層和邏輯層。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.