欢迎光临
我们一直在努力

运用.NET+SQL Server2005构建多层网站(3)-.NET教程,数据库应用

建站超值云服务器,限时71元/月

利用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控件的特性,可以很

方便地沟通表示层和逻辑层。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 运用.NET+SQL Server2005构建多层网站(3)-.NET教程,数据库应用
分享到: 更多 (0)