第二层:哪种性质的操作
第三层:SQL语句的执行
谁有没有更好的方法,望各位能教我下.
支持这种做法,好处是便于修改差错和以后的系统功能升级。
不过要注意的是,这样做的前提是必须设计好良好的代码架构。
比如这种办法
DataAccess.asp
<%
'数据库访问类
Class DataAccess
Dim con
Dim rs
Private ConStr
Public Property Let ConnectionString(vData)
ConStr=vData
End Property
Public Property Get ConnecitonString()
ConnecitonString=ConStr
End Property
Public Sub Init()
Set con=Server.CreateObject("ADODB.Connection")
Set rs=Server.CreateObject("ADODB.RecordSet")
End Sub
Public Sub OpenDataBase()
con.Open ConStr
End Sub
Public Function getRecordSet(Sql,adLock,adCurrent)
rs.Open Sql adLock,adCurrent
Set getRecordSet=rs
End Function
Public Sub ExecuteSql(Sql)
con.Execute Sql
End Sub
Public Sub Close(rs)
rs.Close
con.Close
Set rs=Nothing
Set con=Nothing
End Sub
End Class
%>
<%
'用户类
Class User
Private mUserName
Private mPassword
Private mDBAss
Public Property Let DataAccess(vData)
Set mDBAss=vData
End Property
Public Property Get DataAccess()
Set DataAccess=mDBAss
End Property
Public Property Let UserName(vData)
mUserName=vData
End Property
Public Property Get UserName()
UserName=mUserName
End Property
Public Property Let Password(vData)
mPassword=vData
End Property
Public Property Get Password()
Password=mPassword
End Property
'登陆方法
Public Function Login()
Dim tSql,rs
tSql="select * from [user] where UserName='"&mUserName&"' And Password='"&mPassword&"'"
Set rs=mDBAss.getRecordSet(tSql,1,1)
IF rs.EOF Then
Login=False
Else
Login=True
End IF
mDBAss.Close rs
End Function
End Class
%>
登陆页面
Login.asp
<!--#include file="DataAccess.asp"-->
<!--#include file="User.asp"-->
<%
Dim mDB,oUs
Set mDB=New DataAccess
Set oUs=New User
mDB.ConnectionString="数据库连接字符串"
Call mDB.Init()
Call mDB.OpenDataBase()
oUs.DataAccess=mDB
oUs.UserName=Request.Form("UserName")
oUs.Password=Request.Form("Password")
IF oUser.Login()=False Then
Response.Write "用户名或密码错误"
Else
Response.Redirect "跳转到登陆后的页面"
End IF
%>