手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp编程>列表

建立三层结构的ASP应用程序

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

  "can also be dsn-less string
  conn.ConnectionString="dsn=dsnname;uid=username;password=pwd"
  conn.open
  sql="select * from Employee where EMPLID="" & EMPLID & """
  with rs
   .open sql,conn,1,3
   if .eof and .bof then
     exit sub
   else
     msEMPLID=trim(.Fields("EMPLID"))
     msName=trim(.Fields("Name"))
     msGender=trim(.Fields("Gender"))
     msScore=.Fields("Score")
   end if
   .close
  end with
  set rs=nothing
  conn.close
  set conn=nothing
  End Sub
  这里根据EMPLID创建Employee对象,注意数据库中的值是赋给三个私有变量,而不是直接赋值给属性,如果你单步调试就会发现,给msEMPLID赋值会调用Property Let EMPLID,也就是给属性赋值。
  下面我们再创建一个类Employees,并填加如下方法:
  private colQualifiedList as new Collection
  private mnCurrentIndex as integer
  Public Sub GetQualifiedList()
  dim conn as new Connection
  dim rs as new Recordset
  dim sql as string
  "Suppose that you create a DSN in the control panel, the connectionstring property
  "can also be dsn-less string
  conn.ConnectionString="dsn=dsnname;uid=username;password=pwd"
  conn.open
  sql="select EMPLID from Employee where Score>=60 order by Score desc"
  with rs
   .open sql,conn,1,3
   if .eof and .bof then
     exit sub
   else
     do while not .eof
       dim oEmployee as new Employee
       oEmployee.Create trim(.Fields("EMPLID"))
       colQualifiedList.Add oEmployee
       set oEmployee=nothing
     loop
   end if
   .close
  end with
  set rs=nothing
  conn.close
  set conn=nothing
  End Sub
  首先请注意VB中创建类实例的语法dim oEmployee as new Employee,后面会看到,在ASP中创建类实例的语法是不同的。这个方法检索成绩大于等于60的员工工号,并据此创建一个Employee对象,再将此对象加入私有的集合对象中。下面两个函数遍历集合中的元素:
  Public Function GetFirst() as Employee
   if colQualifiedList.count>0 then
     mnCurrentIndex=1
     set GetFirst=colQualifiedList.Item(1)
   else
     set GetFirst=nothing
   end if
  End Function
  Public Function GetNext() as Employee
   mnCurrentIndex=mnCurrentIndex 1
   if mnCurrentIndex>colQualifiedList.count then
     set GetNext=nothing
   else
     set GetNext=colQualifiedList.Item(mnCurrentIndex)
   End if
  End Function
  也许你会说,为何不把集合声明Public,这样在ASP中不是可以直接引用吗?确实,这样也行得通,编程实现起来也更简单些,但是,这样做破坏了封装性原则。因为数据以何格式存储完全是商业逻辑层的事,与用户界面层无关,假设有一天你因为每种原因放弃了用集合来存储数据的设计,而改用数组或记录集(Recordset)来存储,那你只需要修改GetFirst和GetNext两个函数,用户界面层完全无需修改。
  至此类文件创建完毕,将工程文件存为 test.vbp,选File菜单下的Make test.dll选项将其编译。
  3.注册动态链接库
  启动Web Server 上的Microsoft Transaction Server (Start--Windows NT Optionpack4--Internet Information Server--Internet Service Manager),展开Microsoft Transaction Server--Computer--My Computer--Package Installed,点鼠标右键选New--Package--Create Empty Package,输入包名Test(这里Test是任选的名字,不一定要与DLL同名),OK-Interactive User-the current Logon user--Finish。双击Test--Component,右键选Component-New-Component-Install New component(s)-- Add File,选择你刚编译好的DLL文件,MTS会发现DLL中有两个类Employee和Employees。至此DLL注册完毕。


  4.编写ASP程序
  <HTML><Body>
  <p>Qualified Employee List</p>
  <table border=1 cellspacing=0 cellpadding=0>
  <tr>
   <td>Employee ID</td>
   <td>Name</td>
   <td>Gender</td>
   <td>Score</td>
  </tr>
  <%
   set oEmployees=server.createobject("Test.Employees")
   oEmployees.GetQualifiedList
   set oEmployee=oEmployees.GetFirst()
   do while not oEmployee is nothing
  %>
  <tr>
   <td><%=oEmployee.EMPLID%></td>
   <td><%=oEmployee.Name%></td>
   <td><%=oEmployee.Gender%></td>
   <td><%=oEmployee.Score%></td>
  </tr>
  <%
     set oEmployee=oEmployees.GetNext()
   loop
  %>
  </table>
  </body></html>
  注意在ASP中创建类实例的语法set oEmployees=server.createobject("Test.Employees"),其中Test是DLL的名字,Employees是类的名字; 当然,如果一个函数的返回值是一个对象,类似set oEmployee=oEmployees.GetFirst()这样的语法也是可以的。

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!