標籤:end ref w3c date let highlight mina delete tag
實體類
<%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 PropertyEnd 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 ClassSet 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 ClassSet 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 FunctionEnd ClassSet 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>
asp也玩三層架構(有原始碼)