asp也玩三层架构(有源代码)

实体类

<%
Class UserInfo
    Private mintId
    Public Property Let UserId(intUserId)
        mintId = intUserId
    End Property 
    Public Property Get UserId()
        UserId=mintId
    End Property
    Private mstrName
    Public Property Let UserName(strName)
            mstrName = strName
    End Property

    Public Property Get UserName()
            UserName = mstrName
    End Property
    
    Private mintAge
    Public Property Let UserAge(intAge)
            mintAge=intAge
    End Property
    Public Property Get UserAge()
        UserAge = mintAge
    End Property
End Class
%>

  数据访问层类

<!--#include file="Model.asp"-->
<!--#include file="DBHelper.asp"-->
<%
Class UserDAL

    Public Sub InsertUser(objUserInfo)
        strInsertSql="insert into Users (UserName,UserAge) values ('" &objUserInfo.UserName &_
            "',"& objUserInfo.UserAge &")"
        DB.ExecuteNonQuery(strInsertSql)
    End Sub
    
    Public Sub DeleteUser(intUserId)
        strDeleteSql="delete from Users where UserId="& intUserId
        DB.ExecuteNonQuery(strDeleteSql)
    End Sub
    
    Public Sub UpdateUser(objUserInfo)
        strUpdateSql="update Users set UserName='"& objUserInfo.UserName  &"',UserAge="& objUserInfo.UserAge &_
            " where UserId="& objUserInfo.UserId
        DB.ExecuteNonQuery(strUpdateSql)
    End Sub
    
    Public Function GetAllUser()
        strSelectSql="select * from Users"
        Set rs=DB.ExecuteQuery(strSelectSql)
        Set dic=Server.CreateObject("Scripting.Dictionary")
        While not rs.eof
            Set user=CreateUser(rs) 
            dic.Add user.UserId,user
            rs.MoveNext
        wend
        rs.Close
        Set rs=nothing
        Set GetAllUser=dic
    End Function
    Public Function GetUserById(intUserId)
        strSelectSql="select * from Users where UserId="&intUserId
        Set rs=DB.ExecuteQuery(strSelectSql)
        Set user=CreateUser(rs)
        rs.Close
        Set rs=nothing 
        Set GetUserById=user
        
    End Function
    
    Private Function CreateUser(rs)
        Set user=new UserInfo
        user.UserId=rs("UserId")
        user.UserName=rs("UserName")
        user.UserAge=rs("UserAge")
        Set CreateUser=user
    End Function
    
End Class

Set UserDao=new UserDAL
%>

  用到的DBHelper类

<%
Class DBHelper
    Private conn
    Private Sub Class_Initialize
        strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("DB.mdb") 
        Set conn=Server.CreateObject("ADODB.Connection" )
        conn.Open(strConn)
    End Sub
    Private Sub Class_Terminate
        conn.Close()
        Set conn=nothing
    End Sub
    Public Function ExecuteQuery(strSql)
        Set rs=Server.CreateObject("ADODB.RecordSet")
        rs.Open strSql,conn,1,1
        Set ExecuteQuery=rs
    End Function
    Public Sub ExecuteNonQuery(strSql)
        conn.Execute(strSql)
    End Sub 
End Class
Set DB=new DBHelper
%>

  业务层类

<!--#include file="DAL.asp"-->
<%
Class UserBLL
    Public Function InsertUser(objUserInfo)
        If not IsNumeric(objUserInfo.UserAge) Then
            InsertUser="年龄必需是数字!"
        Else
            UserDao.InsertUser(objUserInfo)
            InsertUser="添加用户成功!"
        End If
    End Function
    Public Function DeleteUser(intUserId)
        If IsNumeric(intUserId) Then
            UserDao.DeleteUser(intUserId)
            DeleteUser="删除用户成功!"
        End If
    End Function
    Public Function UpdateUser(objUserInfo)
        If not IsNumeric(objUserInfo.UserAge) Then
            UpdateUser="年龄必需是数字!"
        Else
            UserDao.UpdateUser(objUserInfo)
            UpdateUser="更新用户成功!"
        End If
    End Function
    Public Function GetAllUser()
        Set GetAllUser=UserDao.GetAllUser()
    End Function
    Public Function GetUserById(intUserId)
        Set GetUserById=UserDao.GetUserById(intUserId)
    End Function
End Class
Set UserManager=new UserBLL
%>

下面是表示层代码
显示所有User

<!--#include file="BLL.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>


<form id="form1" name="form1" method="post" action="DoInsertUser.asp">


  <label>姓名:
  <input type="text" name="Name" />
  年龄:
  <input type="text" name="Age" />
  <input type="submit" name="Submit" value="添加" />
  </label>
  <table width="361" border="1">
  <tr>
    <td width="56">UserId</td>
    <td width="74">UserName</td>
    <td width="65">UserAge</td>
    <td width="73"> </td>
    <td width="59"> </td>
  </tr>
  <%Set users=UserManager.GetAllUser()
  For Each user in users.Items
  %>
    <tr>
    <td><%=user.UserId%></td>
    <td><%=user.UserName%></td>
    <td><%=user.UserAge%></td>
    <td><a href="EditUser.asp?UserId=<%=user.UserId%>">编辑</a></td>
    <td><a href="DoDeleteUser.asp?UserId=<%=user.UserId%>">删除</a></td>
  </tr>
  <%
  Next
  %>

</table>
</form>
</body>
</html>

  

posted @ 2017-08-15 14:37  作巴  阅读(1126)  评论(0编辑  收藏  举报