1 <%
<%
2 Class dbClass
Class dbClass
3 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------
4 '变量说明
    '变量说明
5 'conn-----------connection对象
    'conn-----------connection对象
6 'strsql---------执行查询的语句
    'strsql---------执行查询的语句
7 'vTbName--------查询分页的表名
    'vTbName--------查询分页的表名
8 'vPKey----------查询分页的表的主键
    'vPKey----------查询分页的表的主键
9 'vPgFields------查询分页要显示的字段
    'vPgFields------查询分页要显示的字段
10 'vPgSize--------查询分页每页显示的记录数
    'vPgSize--------查询分页每页显示的记录数
11 'vCurrPg--------查询分页显示的当前页
    'vCurrPg--------查询分页显示的当前页
12 'vConditions----查询分页的条件
    'vConditions----查询分页的条件
13 'vOrderBy-------查询分页的排序
    'vOrderBy-------查询分页的排序
14 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------
15 private conn,strsql,vTbName,vPKey,vPgFields,vPgSize,vCurrPg,vConditions,vOrderBy
    private conn,strsql,vTbName,vPKey,vPgFields,vPgSize,vCurrPg,vConditions,vOrderBy
16 '类的初始化
    '类的初始化
17 private Sub Class_Initialize()
    private Sub Class_Initialize()
18 '当是MS Sql数据库时设置以下两个变量
        '当是MS Sql数据库时设置以下两个变量 
19 'dim dbServer            '数据库服务器的名称或ip地址
        'dim dbServer            '数据库服务器的名称或ip地址
20 'dim dbname              '数据库的名字
        'dim dbname              '数据库的名字
21 dim dbPath              '若是Access数据库,此处设置其路径
        dim dbPath              '若是Access数据库,此处设置其路径
22 dim dbUser              '数据库的登录用户名
        dim dbUser              '数据库的登录用户名
23 dim dbPass              '数据库的登录密码
        dim dbPass              '数据库的登录密码
24 dim connstr
        dim connstr
25 dbPath = "/testasp/data/data.mdb" '设置数据库路径
        dbPath = "/testasp/data/data.mdb" '设置数据库路径
26 dbUser = "admin"
        dbUser = "admin" 
27 dbPass = "123456"
        dbPass = "123456"
28 '若是access,并且有密码
        '若是access,并且有密码
29 connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dbPath) &_
        connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dbPath) &_
30 ";User ID=" & dbUser & ";Password=;Jet OLEDB:Database Password=" & dbPass
         ";User ID=" & dbUser & ";Password=;Jet OLEDB:Database Password=" & dbPass
31 '若是access,并且没有密码
        '若是access,并且没有密码
32 'connstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(dbPath)
        'connstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(dbPath)
33 '若是ms-sql数据库
        '若是ms-sql数据库
34 'connstr = "Provider = Sqloledb; User ID = " & dbUser & "; Password = " & dbPass &_
        'connstr = "Provider = Sqloledb; User ID = " & dbUser & "; Password = " & dbPass &_
35 ' "; Initial Catalog = " & dbname & "; Data Source = " & dbServer
        ' "; Initial Catalog = " & dbname & "; Data Source = " & dbServer 
36 on error resume next
        on error resume next
37 set conn=server.CreateObject("adodb.connection")
        set conn=server.CreateObject("adodb.connection")
38 conn.open connstr
        conn.open connstr
39 errMsg "连接数据库"
        errMsg "连接数据库"
40 End Sub
    End Sub
41 '类结束
    '类结束
42 Private Sub Class_terminate()
    Private Sub Class_terminate()
43 conn.close
        conn.close
44 set conn=nothing
        set conn=nothing
45 End Sub
    End Sub
46 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------
47 '给类的变量设置值
'给类的变量设置值
48 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------    
49 '设置sql语句
    '设置sql语句
50 Public Property Let sqlStr(Byval Values)
    Public Property Let sqlStr(Byval Values)
51 strsql=Values
    strsql=Values
52 End Property
    End Property
53 '设置查询分页的表名
    '设置查询分页的表名
54 public property let tbName(Byval Values)
    public property let tbName(Byval Values) 
55 vTbName=Values
    vTbName=Values
56 end property
    end property
57 '--------------------------------------------------------
    '--------------------------------------------------------
58 '设置查询分页的表的主键
    '设置查询分页的表的主键
59 public property let pKey(ByVal Values)
    public property let pKey(ByVal Values)
60 vPKey=Values
    vPKey=Values
61 end property
    end property
62 '--------------------------------------------------------
    '--------------------------------------------------------
63 '设置显示的字段
    '设置显示的字段
64 public property let pgFields(ByVal Values)
    public property let pgFields(ByVal Values)
65 vPgFields=Values
    vPgFields=Values
66 end property
    end property
67 '--------------------------------------------------------
    '--------------------------------------------------------
68 '设置每页显示的记录数
    '设置每页显示的记录数
69 public property let pgSize(ByVal Values)
    public property let pgSize(ByVal Values)
70 vPgSize=Values
    vPgSize=Values
71 end property
    end property
72 '---------------------------------------------------------
    '---------------------------------------------------------
73 '设置当前显示的页数
    '设置当前显示的页数
74 public property let currPg(ByVal Values)
    public property let currPg(ByVal Values) 
75 vCurrPg=Values
    vCurrPg=Values
76 end property
    end property
77 '--------------------------------------------------------
    '--------------------------------------------------------
78 '设置查询的条件
    '设置查询的条件
79 public property let conditions(ByVal Values)
    public property let conditions(ByVal Values)
80 if Len(Values)>0 then
    if Len(Values)>0 then
81 vConditions=" where "&Values
    vConditions=" where "&Values
82 else
    else
83 vConditions=" where 1=1 "
    vConditions=" where 1=1 "
84 end if
    end if
85 end property
    end property
86 '-------------------------------------------------------
    '-------------------------------------------------------
87 '设置查询的排序
    '设置查询的排序
88 public property let orderBy(ByVal Values)
    public property let orderBy(ByVal Values)
89 if Len(Values)>0 then
    if Len(Values)>0 then
90 vOrderBy=" order by "&Values
    vOrderBy=" order by "&Values
91 else
    else
92 vOrderBy=Values
    vOrderBy=Values
93 end if
    end if
94 end property
    end property    
95 '-------------------------------------------------------------
    '------------------------------------------------------------- 
96 '得到记录总数
    '得到记录总数
97 public property get vRsCount()
    public property get vRsCount()
98 if vCurrPg=1 then
    if vCurrPg=1 then
99 sqlc="select count("&vPKey&") as Idcount from "&vTbName&" "&vConditions
    sqlc="select count("&vPKey&") as Idcount from "&vTbName&" "&vConditions
100 set rsc=server.CreateObject("adodb.recordset")
    set rsc=server.CreateObject("adodb.recordset")
101 rsc.open sqlc,conn,0,1
    rsc.open sqlc,conn,0,1
102 RsNum=rsc("IdCount")
    RsNum=rsc("IdCount")
103 rsc.close
    rsc.close
104 set rsc=nothing
    set rsc=nothing
105 if RsNum>0 then
    if RsNum>0 then
106 response.Cookies("iRecord")=RsNum
    response.Cookies("iRecord")=RsNum
107 vRsCount=RsNum
    vRsCount=RsNum
108 else
    else
109 vRsCount=0
    vRsCount=0
110 end if
    end if
111 else
    else
112 vRsCount=request.Cookies("iRecord")
    vRsCount=request.Cookies("iRecord")
113 end if
    end if
114 end property
    end property
115 '得到总页数
    '得到总页数
116 public property get vPgCount()
    public property get vPgCount() 
117 iRsCount2=vRsCount()
    iRsCount2=vRsCount()
118 if iRsCount2 mod vPgSize =0 then
    if iRsCount2 mod vPgSize =0 then
119 vPgCount=int(iRsCount2/vPgSize)
    vPgCount=int(iRsCount2/vPgSize)
120 else
    else
121 vPgCount=int(iRsCount2/vPgSize)+1
    vPgCount=int(iRsCount2/vPgSize)+1
122 end if
    end if
123 end property
    end property
124 '查询数据库
'查询数据库
125 Public Function rsDB()
    Public Function rsDB()
126 on error resume next
    on error resume next
127 '简单的查询出结果
    '简单的查询出结果
128 '     set rsDB = Server.CreateObject("ADODB.RecordSet")
'     set rsDB = Server.CreateObject("ADODB.RecordSet")
129 '     rsDB.Open strsql,conn,1,3
'     rsDB.Open strsql,conn,1,3 
130 Set rsDB=conn.Execute(strsql)
    Set rsDB=conn.Execute(strsql)
131 errMsg "查询数据库"
    errMsg "查询数据库"
132 End Function
    End Function
133 '添加,更新,删除数据库记录
'添加,更新,删除数据库记录
134 public Function upDB()
    public Function upDB()
135 on error resume next
    on error resume next
136 conn.execute(strsql)
    conn.execute(strsql)
137 errMsg "编辑数据库记录"
    errMsg "编辑数据库记录"
138 end Function
    end Function
139 '-------------------------------------------------------------------------
'------------------------------------------------------------------------- 
140 '用来实现分页的记录集函数
'用来实现分页的记录集函数
141 public function pageRs()
    public function pageRs()
142 on error resume next
    on error resume next
143 dim startRs
    dim startRs
144 startRs=(vCurrPg-1)*vPgSize
    startRs=(vCurrPg-1)*vPgSize
145 '-------------------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------------------
146 '使用此语句的话要根据参数修改代码,具体的是若排序为asc则<改为>,min改为max
    '使用此语句的话要根据参数修改代码,具体的是若排序为asc则<改为>,min改为max
147 '    if startRs=0 then
'    if startRs=0 then
148 '    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy
'    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy
149 '    else
'    else
150 '    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" < "
'    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" < "
151 '    strsql=strsql&"(select min("&vPKey&") from (select top "&startRs&" "&vPKey&" from "
'    strsql=strsql&"(select min("&vPKey&") from (select top "&startRs&" "&vPKey&" from " 
152 '    strsql=strsql&vTbName&" "&vConditions&" "&vOrderBy&") as idTable) "&vOrderBy
'    strsql=strsql&vTbName&" "&vConditions&" "&vOrderBy&") as idTable) "&vOrderBy
153 '    end if
'    end if
154 '---------------------------------------------------------------
    '---------------------------------------------------------------
155 if startRs=0 then
    if startRs=0 then
156 strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy
    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy
157 else
    else
158 strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" not "
    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" not "
159 strsql=strsql&"in (select top "&startRs&" "&vPKey&" from "&vTbName&" "&vConditions&" "&vOrderBy
    strsql=strsql&"in (select top "&startRs&" "&vPKey&" from "&vTbName&" "&vConditions&" "&vOrderBy
160 strsql=strsql&") "&vOrderBy
    strsql=strsql&") "&vOrderBy 
161 end if
    end if
162 '-------------------------------------------------------------------
    '-------------------------------------------------------------------
163 set pageRs=server.CreateObject("adodb.recordset")
    set pageRs=server.CreateObject("adodb.recordset")
164 pageRs.open strsql,conn,0,1
    pageRs.open strsql,conn,0,1
165 errMsg "记录分页"
    errMsg "记录分页"
166 end function
    end function
167 '------sql用存储过程分页------------------------------------------------------
'------sql用存储过程分页------------------------------------------------------
168 public function sqlPage()
public function sqlPage()
169 on error resume next
on error resume next
170 Set sqlPage=server.CreateObject("Adodb.RecordSet")
Set sqlPage=server.CreateObject("Adodb.RecordSet")
171 Set Cm=Server.CreateObject("Adodb.Command")
Set Cm=Server.CreateObject("Adodb.Command")
172 Cm.CommandType = 4
Cm.CommandType = 4
173 Cm.ActiveConnection = conn
Cm.ActiveConnection = conn
174 Cm.CommandText="sp_Util_Page"
Cm.CommandText="sp_Util_Page"
175 Cm.parameters(1) = vPgFields
Cm.parameters(1) = vPgFields
176 Cm.parameters(2) = vTbName
Cm.parameters(2) = vTbName
177 Cm.parameters(3) = vConditions
Cm.parameters(3) = vConditions
178 Cm.parameters(4) = vOrderBy
Cm.parameters(4) = vOrderBy
179 Cm.parameters(5) = vPKey
Cm.parameters(5) = vPKey
180 Cm.parameters(6) = vCurrPg
Cm.parameters(6) = vCurrPg
181 Cm.parameters(7) = vPgSize
Cm.parameters(7) = vPgSize
182 Cm.parameters(8) = vRsCount()
Cm.parameters(8) = vRsCount()
183 Cm.parameters(9) = ""
Cm.parameters(9) = ""
184 sqlPage.CursorLocation = 3
sqlPage.CursorLocation = 3 
185 sqlPage.LockType = 1
sqlPage.LockType = 1
186 sqlPage.Open Cm
sqlPage.Open Cm
187 errMsg "记录分页"
errMsg "记录分页"
188 end function
end function
189 '----------------------------------------------------------------------------
'----------------------------------------------------------------------------
190 '关闭记录集objRs
'关闭记录集objRs
191 '----------------------------------------------------------------------------
'----------------------------------------------------------------------------
192 Public Function cRs(ByVal ObjRs)
    Public Function cRs(ByVal ObjRs)
193 ObjRs.close()
    ObjRs.close()
194 Set ObjRs = Nothing
    Set ObjRs = Nothing
195 End Function
    End Function
196 '----------------------分页的页码导航---------------------------------------
'----------------------分页的页码导航---------------------------------------
197 public function pageNav()
public function pageNav()
198 iRsCount=vRsCount()'总记录数
iRsCount=vRsCount()'总记录数
199 mypage=vCurrPg'当前页数
mypage=vCurrPg'当前页数
200 PgCount=vPgCount()'总页数
PgCount=vPgCount()'总页数
201 prePage=mypage-1
prePage=mypage-1
202 if prePage<1 then
if prePage<1 then
203 prePage=1
prePage=1
204 end if
end if
205 nextPage=mypage+1
nextPage=mypage+1
206 if nextPage>PgCount then
if nextPage>PgCount then
207 nextPage=PgCount
nextPage=PgCount
208 end if
end if
209 pagestr="<div id=""fy""><span id=""rpc"">总共有"&iRsCount&"条记录 "&mypage&"/"&PgCount&"</span>"
pagestr="<div id=""fy""><span id=""rpc"">总共有"&iRsCount&"条记录 "&mypage&"/"&PgCount&"</span>" 
210 pagestr=pagestr&"<a href='?currpage=1' class='aW'>首页</a><a href='?currpage="&prePage&"' class='aW'>前一页</a>"
pagestr=pagestr&"<a href='?currpage=1' class='aW'>首页</a><a href='?currpage="&prePage&"' class='aW'>前一页</a>"
211 if (mypage-1) mod 4=0 then
if (mypage-1) mod 4=0 then
212 firstPage=mypage
firstPage=mypage
213 elseif int((mypage-1)/4)=0 then
elseif int((mypage-1)/4)=0 then
214 firstPage=1
firstPage=1
215 else
else
216 firstPage=int((mypage-1)/4)*4+1
firstPage=int((mypage-1)/4)*4+1
217 end if
end if
218 endPage=firstPage+4
endPage=firstPage+4
219 astr=""
astr=""
220 for i=firstPage to endPage
for i=firstPage to endPage
221 astr=astr&"<a href='?currpage="&i&"'"
astr=astr&"<a href='?currpage="&i&"'"
222 if Cstr(mypage)=Cstr(i) then
if Cstr(mypage)=Cstr(i) then
223 astr=astr&" id='currP'"
astr=astr&" id='currP'"
224 end if
end if
225 astr=astr&">"&i&"</a>"
astr=astr&">"&i&"</a>"
226 if i>PgCount-1 then exit for
if i>PgCount-1 then exit for
227 next
next
228 astr=astr&"<a href='?currpage="&nextPage&"' class='aW'>后一页</a><a href='?currpage="&PgCount&"' class='aW'>尾页</a></div>"
astr=astr&"<a href='?currpage="&nextPage&"' class='aW'>后一页</a><a href='?currpage="&PgCount&"' class='aW'>尾页</a></div>"
229 pagestr=pagestr&astr
pagestr=pagestr&astr
230 pageNav=pagestr
pageNav=pagestr
231 end function
end function
232 '输出带分页功能的table
'输出带分页功能的table
233 Function showTb(ByVal TbTil)
Function showTb(ByVal TbTil) 
234 set rsTb=pageRs()'若是存储过程就调用sqlPage()
    set rsTb=pageRs()'若是存储过程就调用sqlPage()
235 tbRs= rsTb.getrows()
    tbRs= rsTb.getrows()
236 cRs(rsTb)
    cRs(rsTb)
237 iTblRow=Ubound(tbRs,2)
    iTblRow=Ubound(tbRs,2)
238 iTblCol=Ubound(TbTil)
    iTblCol=Ubound(TbTil)
239 tbStr="<table border='0' cellspacing='0' cellpadding='0'><tbody>"
    tbStr="<table border='0' cellspacing='0' cellpadding='0'><tbody>"
240 for r1=0 to iTblCol
    for r1=0 to iTblCol
241 tr1=tr1&"<td width='"&split(TbTil(r1),"|")(1)&"'>"&split(TbTil(r1),"|")(0)&"</td>"
      tr1=tr1&"<td width='"&split(TbTil(r1),"|")(1)&"'>"&split(TbTil(r1),"|")(0)&"</td>"
242 next
    next
243 tr1="<tr>"&tr1&"</tr>"
    tr1="<tr>"&tr1&"</tr>"
244 for ri=0 to iTblRow
    for ri=0 to iTblRow
245 for ci=0 to iTblCol
      for ci=0 to iTblCol
246 td=td&"<td width='"&split(TbTil(ci),"|")(1)&"'>"&tbRs(ci,ri)&"</td>"
        td=td&"<td width='"&split(TbTil(ci),"|")(1)&"'>"&tbRs(ci,ri)&"</td>"
247 next
      next 
248 tr=tr&"<tr>"&td&"</tr>"
      tr=tr&"<tr>"&td&"</tr>"
249 td=null
      td=null
250 next
    next
251 TbTil=null
    TbTil=null
252 tbRs=null
    tbRs=null
253 response.Write(tbStr&tr1&tr&"<tr><td colspan='"&iTblCol+1&"'>"&pageNav()&"</td></tr></tbody></table>")
    response.Write(tbStr&tr1&tr&"<tr><td colspan='"&iTblCol+1&"'>"&pageNav()&"</td></tr></tbody></table>")
254 tbStr=null
    tbStr=null
255 tr1=null
    tr1=null
256 tr=null
    tr=null
257 End Function
End Function
258 '打印sql语句,以便语句有错误时检查
'打印sql语句,以便语句有错误时检查
259 Public Sub prnSql()
    Public Sub prnSql()
260 response.Write(strsql)
        response.Write(strsql)
261 End Sub
    End Sub
262 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------    
263 '容错函数
'容错函数
264 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------
265 Private Function errMsg(errMsg)
     Private Function errMsg(errMsg) 
266 If Err.number<>0 Then
     If Err.number<>0 Then
267 '出现问题可利用此处代码打印出描述信息,方便调试。可注释掉
     '出现问题可利用此处代码打印出描述信息,方便调试。可注释掉
268 response.Write(Cstr(Err.description)&"<br>")
     response.Write(Cstr(Err.description)&"<br>")
269 Err.Clear
     Err.Clear
270 Response.Write "<font color='#FF0000'>"&errMsg&"出错</font>" '注释
     Response.Write "<font color='#FF0000'>"&errMsg&"出错</font>" '注释
271 Response.End()
     Response.End()
272 End If
     End If
273 End Function
     End Function
274 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------
275 '容错函数结束
'容错函数结束
276 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------
277 End Class
End Class
278 %>
%>
 <%
<%2
 Class dbClass
Class dbClass3
 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------4
 '变量说明
    '变量说明5
 'conn-----------connection对象
    'conn-----------connection对象6
 'strsql---------执行查询的语句
    'strsql---------执行查询的语句7
 'vTbName--------查询分页的表名
    'vTbName--------查询分页的表名8
 'vPKey----------查询分页的表的主键
    'vPKey----------查询分页的表的主键9
 'vPgFields------查询分页要显示的字段
    'vPgFields------查询分页要显示的字段10
 'vPgSize--------查询分页每页显示的记录数
    'vPgSize--------查询分页每页显示的记录数11
 'vCurrPg--------查询分页显示的当前页
    'vCurrPg--------查询分页显示的当前页12
 'vConditions----查询分页的条件
    'vConditions----查询分页的条件13
 'vOrderBy-------查询分页的排序
    'vOrderBy-------查询分页的排序14
 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------15
 private conn,strsql,vTbName,vPKey,vPgFields,vPgSize,vCurrPg,vConditions,vOrderBy
    private conn,strsql,vTbName,vPKey,vPgFields,vPgSize,vCurrPg,vConditions,vOrderBy16
 '类的初始化
    '类的初始化17
 private Sub Class_Initialize()
    private Sub Class_Initialize()18
 '当是MS Sql数据库时设置以下两个变量
        '当是MS Sql数据库时设置以下两个变量 19
 'dim dbServer            '数据库服务器的名称或ip地址
        'dim dbServer            '数据库服务器的名称或ip地址20
 'dim dbname              '数据库的名字
        'dim dbname              '数据库的名字21
 dim dbPath              '若是Access数据库,此处设置其路径
        dim dbPath              '若是Access数据库,此处设置其路径22
 dim dbUser              '数据库的登录用户名
        dim dbUser              '数据库的登录用户名23
 dim dbPass              '数据库的登录密码
        dim dbPass              '数据库的登录密码24
 dim connstr
        dim connstr25
 dbPath = "/testasp/data/data.mdb" '设置数据库路径
        dbPath = "/testasp/data/data.mdb" '设置数据库路径26
 dbUser = "admin"
        dbUser = "admin" 27
 dbPass = "123456"
        dbPass = "123456"28
 '若是access,并且有密码
        '若是access,并且有密码29
 connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dbPath) &_
        connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dbPath) &_30
 ";User ID=" & dbUser & ";Password=;Jet OLEDB:Database Password=" & dbPass
         ";User ID=" & dbUser & ";Password=;Jet OLEDB:Database Password=" & dbPass31
 '若是access,并且没有密码
        '若是access,并且没有密码32
 'connstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(dbPath)
        'connstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(dbPath)33
 '若是ms-sql数据库
        '若是ms-sql数据库34
 'connstr = "Provider = Sqloledb; User ID = " & dbUser & "; Password = " & dbPass &_
        'connstr = "Provider = Sqloledb; User ID = " & dbUser & "; Password = " & dbPass &_35
 ' "; Initial Catalog = " & dbname & "; Data Source = " & dbServer
        ' "; Initial Catalog = " & dbname & "; Data Source = " & dbServer 36
 on error resume next
        on error resume next37
 set conn=server.CreateObject("adodb.connection")
        set conn=server.CreateObject("adodb.connection")38
 conn.open connstr
        conn.open connstr39
 errMsg "连接数据库"
        errMsg "连接数据库"40
 End Sub
    End Sub41
 '类结束
    '类结束42
 Private Sub Class_terminate()
    Private Sub Class_terminate()43
 conn.close
        conn.close44
 set conn=nothing
        set conn=nothing45
 End Sub
    End Sub46
 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------47
 '给类的变量设置值
'给类的变量设置值48
 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------    49
 '设置sql语句
    '设置sql语句50
 Public Property Let sqlStr(Byval Values)
    Public Property Let sqlStr(Byval Values)51
 strsql=Values
    strsql=Values52
 End Property
    End Property53
 '设置查询分页的表名
    '设置查询分页的表名54
 public property let tbName(Byval Values)
    public property let tbName(Byval Values) 55
 vTbName=Values
    vTbName=Values56
 end property
    end property57
 '--------------------------------------------------------
    '--------------------------------------------------------58
 '设置查询分页的表的主键
    '设置查询分页的表的主键59
 public property let pKey(ByVal Values)
    public property let pKey(ByVal Values)60
 vPKey=Values
    vPKey=Values61
 end property
    end property62
 '--------------------------------------------------------
    '--------------------------------------------------------63
 '设置显示的字段
    '设置显示的字段64
 public property let pgFields(ByVal Values)
    public property let pgFields(ByVal Values)65
 vPgFields=Values
    vPgFields=Values66
 end property
    end property67
 '--------------------------------------------------------
    '--------------------------------------------------------68
 '设置每页显示的记录数
    '设置每页显示的记录数69
 public property let pgSize(ByVal Values)
    public property let pgSize(ByVal Values)70
 vPgSize=Values
    vPgSize=Values71
 end property
    end property72
 '---------------------------------------------------------
    '---------------------------------------------------------73
 '设置当前显示的页数
    '设置当前显示的页数74
 public property let currPg(ByVal Values)
    public property let currPg(ByVal Values) 75
 vCurrPg=Values
    vCurrPg=Values76
 end property
    end property77
 '--------------------------------------------------------
    '--------------------------------------------------------78
 '设置查询的条件
    '设置查询的条件79
 public property let conditions(ByVal Values)
    public property let conditions(ByVal Values)80
 if Len(Values)>0 then
    if Len(Values)>0 then81
 vConditions=" where "&Values
    vConditions=" where "&Values82
 else
    else83
 vConditions=" where 1=1 "
    vConditions=" where 1=1 "84
 end if
    end if85
 end property
    end property86
 '-------------------------------------------------------
    '-------------------------------------------------------87
 '设置查询的排序
    '设置查询的排序88
 public property let orderBy(ByVal Values)
    public property let orderBy(ByVal Values)89
 if Len(Values)>0 then
    if Len(Values)>0 then90
 vOrderBy=" order by "&Values
    vOrderBy=" order by "&Values91
 else
    else92
 vOrderBy=Values
    vOrderBy=Values93
 end if
    end if94
 end property
    end property    95
 '-------------------------------------------------------------
    '------------------------------------------------------------- 96
 '得到记录总数
    '得到记录总数97
 public property get vRsCount()
    public property get vRsCount()98
 if vCurrPg=1 then
    if vCurrPg=1 then99
 sqlc="select count("&vPKey&") as Idcount from "&vTbName&" "&vConditions
    sqlc="select count("&vPKey&") as Idcount from "&vTbName&" "&vConditions100
 set rsc=server.CreateObject("adodb.recordset")
    set rsc=server.CreateObject("adodb.recordset")101
 rsc.open sqlc,conn,0,1
    rsc.open sqlc,conn,0,1102
 RsNum=rsc("IdCount")
    RsNum=rsc("IdCount")103
 rsc.close
    rsc.close104
 set rsc=nothing
    set rsc=nothing105
 if RsNum>0 then
    if RsNum>0 then106
 response.Cookies("iRecord")=RsNum
    response.Cookies("iRecord")=RsNum107
 vRsCount=RsNum
    vRsCount=RsNum108
 else
    else109
 vRsCount=0
    vRsCount=0110
 end if
    end if111
 else
    else112
 vRsCount=request.Cookies("iRecord")
    vRsCount=request.Cookies("iRecord")113
 end if
    end if114
 end property
    end property115
 '得到总页数
    '得到总页数116
 public property get vPgCount()
    public property get vPgCount() 117
 iRsCount2=vRsCount()
    iRsCount2=vRsCount()118
 if iRsCount2 mod vPgSize =0 then
    if iRsCount2 mod vPgSize =0 then119
 vPgCount=int(iRsCount2/vPgSize)
    vPgCount=int(iRsCount2/vPgSize)120
 else
    else121
 vPgCount=int(iRsCount2/vPgSize)+1
    vPgCount=int(iRsCount2/vPgSize)+1122
 end if
    end if123
 end property
    end property124
 '查询数据库
'查询数据库125
 Public Function rsDB()
    Public Function rsDB()126
 on error resume next
    on error resume next127
 '简单的查询出结果
    '简单的查询出结果128
 '     set rsDB = Server.CreateObject("ADODB.RecordSet")
'     set rsDB = Server.CreateObject("ADODB.RecordSet")129
 '     rsDB.Open strsql,conn,1,3
'     rsDB.Open strsql,conn,1,3 130
 Set rsDB=conn.Execute(strsql)
    Set rsDB=conn.Execute(strsql)131
 errMsg "查询数据库"
    errMsg "查询数据库"132
 End Function
    End Function133
 '添加,更新,删除数据库记录
'添加,更新,删除数据库记录134
 public Function upDB()
    public Function upDB()135
 on error resume next
    on error resume next136
 conn.execute(strsql)
    conn.execute(strsql)137
 errMsg "编辑数据库记录"
    errMsg "编辑数据库记录"138
 end Function
    end Function139
 '-------------------------------------------------------------------------
'------------------------------------------------------------------------- 140
 '用来实现分页的记录集函数
'用来实现分页的记录集函数141
 public function pageRs()
    public function pageRs()142
 on error resume next
    on error resume next143
 dim startRs
    dim startRs144
 startRs=(vCurrPg-1)*vPgSize
    startRs=(vCurrPg-1)*vPgSize145
 '-------------------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------------------146
 '使用此语句的话要根据参数修改代码,具体的是若排序为asc则<改为>,min改为max
    '使用此语句的话要根据参数修改代码,具体的是若排序为asc则<改为>,min改为max147
 '    if startRs=0 then
'    if startRs=0 then148
 '    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy
'    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy149
 '    else
'    else150
 '    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" < "
'    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" < "151
 '    strsql=strsql&"(select min("&vPKey&") from (select top "&startRs&" "&vPKey&" from "
'    strsql=strsql&"(select min("&vPKey&") from (select top "&startRs&" "&vPKey&" from " 152
 '    strsql=strsql&vTbName&" "&vConditions&" "&vOrderBy&") as idTable) "&vOrderBy
'    strsql=strsql&vTbName&" "&vConditions&" "&vOrderBy&") as idTable) "&vOrderBy153
 '    end if
'    end if154
 '---------------------------------------------------------------
    '---------------------------------------------------------------155
 if startRs=0 then
    if startRs=0 then156
 strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy
    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" "&vOrderBy157
 else
    else158
 strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" not "
    strsql="select top "&vPgSize&" "&vPgFields&" from "&vTbName&" "&vConditions&" and "&vPKey&" not "159
 strsql=strsql&"in (select top "&startRs&" "&vPKey&" from "&vTbName&" "&vConditions&" "&vOrderBy
    strsql=strsql&"in (select top "&startRs&" "&vPKey&" from "&vTbName&" "&vConditions&" "&vOrderBy160
 strsql=strsql&") "&vOrderBy
    strsql=strsql&") "&vOrderBy 161
 end if
    end if162
 '-------------------------------------------------------------------
    '-------------------------------------------------------------------163
 set pageRs=server.CreateObject("adodb.recordset")
    set pageRs=server.CreateObject("adodb.recordset")164
 pageRs.open strsql,conn,0,1
    pageRs.open strsql,conn,0,1165
 errMsg "记录分页"
    errMsg "记录分页"166
 end function
    end function167
 '------sql用存储过程分页------------------------------------------------------
'------sql用存储过程分页------------------------------------------------------168
 public function sqlPage()
public function sqlPage()169
 on error resume next
on error resume next170
 Set sqlPage=server.CreateObject("Adodb.RecordSet")
Set sqlPage=server.CreateObject("Adodb.RecordSet")171
 Set Cm=Server.CreateObject("Adodb.Command")
Set Cm=Server.CreateObject("Adodb.Command")172
 Cm.CommandType = 4
Cm.CommandType = 4173
 Cm.ActiveConnection = conn
Cm.ActiveConnection = conn174
 Cm.CommandText="sp_Util_Page"
Cm.CommandText="sp_Util_Page"175
 Cm.parameters(1) = vPgFields
Cm.parameters(1) = vPgFields176
 Cm.parameters(2) = vTbName
Cm.parameters(2) = vTbName177
 Cm.parameters(3) = vConditions
Cm.parameters(3) = vConditions178
 Cm.parameters(4) = vOrderBy
Cm.parameters(4) = vOrderBy179
 Cm.parameters(5) = vPKey
Cm.parameters(5) = vPKey180
 Cm.parameters(6) = vCurrPg
Cm.parameters(6) = vCurrPg181
 Cm.parameters(7) = vPgSize
Cm.parameters(7) = vPgSize182
 Cm.parameters(8) = vRsCount()
Cm.parameters(8) = vRsCount()183
 Cm.parameters(9) = ""
Cm.parameters(9) = ""184
 sqlPage.CursorLocation = 3
sqlPage.CursorLocation = 3 185
 sqlPage.LockType = 1
sqlPage.LockType = 1186
 sqlPage.Open Cm
sqlPage.Open Cm187
 errMsg "记录分页"
errMsg "记录分页"188
 end function
end function189
 '----------------------------------------------------------------------------
'----------------------------------------------------------------------------190
 '关闭记录集objRs
'关闭记录集objRs191
 '----------------------------------------------------------------------------
'----------------------------------------------------------------------------192
 Public Function cRs(ByVal ObjRs)
    Public Function cRs(ByVal ObjRs)193
 ObjRs.close()
    ObjRs.close()194
 Set ObjRs = Nothing
    Set ObjRs = Nothing195
 End Function
    End Function196
 '----------------------分页的页码导航---------------------------------------
'----------------------分页的页码导航---------------------------------------197
 public function pageNav()
public function pageNav()198
 iRsCount=vRsCount()'总记录数
iRsCount=vRsCount()'总记录数199
 mypage=vCurrPg'当前页数
mypage=vCurrPg'当前页数200
 PgCount=vPgCount()'总页数
PgCount=vPgCount()'总页数201
 prePage=mypage-1
prePage=mypage-1202
 if prePage<1 then
if prePage<1 then203
 prePage=1
prePage=1204
 end if
end if205
 nextPage=mypage+1
nextPage=mypage+1206
 if nextPage>PgCount then
if nextPage>PgCount then207
 nextPage=PgCount
nextPage=PgCount208
 end if
end if209
 pagestr="<div id=""fy""><span id=""rpc"">总共有"&iRsCount&"条记录 "&mypage&"/"&PgCount&"</span>"
pagestr="<div id=""fy""><span id=""rpc"">总共有"&iRsCount&"条记录 "&mypage&"/"&PgCount&"</span>" 210
 pagestr=pagestr&"<a href='?currpage=1' class='aW'>首页</a><a href='?currpage="&prePage&"' class='aW'>前一页</a>"
pagestr=pagestr&"<a href='?currpage=1' class='aW'>首页</a><a href='?currpage="&prePage&"' class='aW'>前一页</a>"211
 if (mypage-1) mod 4=0 then
if (mypage-1) mod 4=0 then212
 firstPage=mypage
firstPage=mypage213
 elseif int((mypage-1)/4)=0 then
elseif int((mypage-1)/4)=0 then214
 firstPage=1
firstPage=1215
 else
else216
 firstPage=int((mypage-1)/4)*4+1
firstPage=int((mypage-1)/4)*4+1217
 end if
end if218
 endPage=firstPage+4
endPage=firstPage+4219
 astr=""
astr=""220
 for i=firstPage to endPage
for i=firstPage to endPage221
 astr=astr&"<a href='?currpage="&i&"'"
astr=astr&"<a href='?currpage="&i&"'"222
 if Cstr(mypage)=Cstr(i) then
if Cstr(mypage)=Cstr(i) then223
 astr=astr&" id='currP'"
astr=astr&" id='currP'"224
 end if
end if225
 astr=astr&">"&i&"</a>"
astr=astr&">"&i&"</a>"226
 if i>PgCount-1 then exit for
if i>PgCount-1 then exit for227
 next
next228
 astr=astr&"<a href='?currpage="&nextPage&"' class='aW'>后一页</a><a href='?currpage="&PgCount&"' class='aW'>尾页</a></div>"
astr=astr&"<a href='?currpage="&nextPage&"' class='aW'>后一页</a><a href='?currpage="&PgCount&"' class='aW'>尾页</a></div>"229
 pagestr=pagestr&astr
pagestr=pagestr&astr230
 pageNav=pagestr
pageNav=pagestr231
 end function
end function232
 '输出带分页功能的table
'输出带分页功能的table233
 Function showTb(ByVal TbTil)
Function showTb(ByVal TbTil) 234
 set rsTb=pageRs()'若是存储过程就调用sqlPage()
    set rsTb=pageRs()'若是存储过程就调用sqlPage()235
 tbRs= rsTb.getrows()
    tbRs= rsTb.getrows()236
 cRs(rsTb)
    cRs(rsTb)237
 iTblRow=Ubound(tbRs,2)
    iTblRow=Ubound(tbRs,2)238
 iTblCol=Ubound(TbTil)
    iTblCol=Ubound(TbTil)239
 tbStr="<table border='0' cellspacing='0' cellpadding='0'><tbody>"
    tbStr="<table border='0' cellspacing='0' cellpadding='0'><tbody>"240
 for r1=0 to iTblCol
    for r1=0 to iTblCol241
 tr1=tr1&"<td width='"&split(TbTil(r1),"|")(1)&"'>"&split(TbTil(r1),"|")(0)&"</td>"
      tr1=tr1&"<td width='"&split(TbTil(r1),"|")(1)&"'>"&split(TbTil(r1),"|")(0)&"</td>"242
 next
    next243
 tr1="<tr>"&tr1&"</tr>"
    tr1="<tr>"&tr1&"</tr>"244
 for ri=0 to iTblRow
    for ri=0 to iTblRow245
 for ci=0 to iTblCol
      for ci=0 to iTblCol246
 td=td&"<td width='"&split(TbTil(ci),"|")(1)&"'>"&tbRs(ci,ri)&"</td>"
        td=td&"<td width='"&split(TbTil(ci),"|")(1)&"'>"&tbRs(ci,ri)&"</td>"247
 next
      next 248
 tr=tr&"<tr>"&td&"</tr>"
      tr=tr&"<tr>"&td&"</tr>"249
 td=null
      td=null250
 next
    next251
 TbTil=null
    TbTil=null252
 tbRs=null
    tbRs=null253
 response.Write(tbStr&tr1&tr&"<tr><td colspan='"&iTblCol+1&"'>"&pageNav()&"</td></tr></tbody></table>")
    response.Write(tbStr&tr1&tr&"<tr><td colspan='"&iTblCol+1&"'>"&pageNav()&"</td></tr></tbody></table>")254
 tbStr=null
    tbStr=null255
 tr1=null
    tr1=null256
 tr=null
    tr=null257
 End Function
End Function258
 '打印sql语句,以便语句有错误时检查
'打印sql语句,以便语句有错误时检查259
 Public Sub prnSql()
    Public Sub prnSql()260
 response.Write(strsql)
        response.Write(strsql)261
 End Sub
    End Sub262
 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------    263
 '容错函数
'容错函数264
 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------265
 Private Function errMsg(errMsg)
     Private Function errMsg(errMsg) 266
 If Err.number<>0 Then
     If Err.number<>0 Then267
 '出现问题可利用此处代码打印出描述信息,方便调试。可注释掉
     '出现问题可利用此处代码打印出描述信息,方便调试。可注释掉268
 response.Write(Cstr(Err.description)&"<br>")
     response.Write(Cstr(Err.description)&"<br>")269
 Err.Clear
     Err.Clear270
 Response.Write "<font color='#FF0000'>"&errMsg&"出错</font>" '注释
     Response.Write "<font color='#FF0000'>"&errMsg&"出错</font>" '注释271
 Response.End()
     Response.End()272
 End If
     End If273
 End Function
     End Function274
 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------275
 '容错函数结束
'容错函数结束276
 '-------------------------------------------------------------------------
'-------------------------------------------------------------------------277
 End Class
End Class278
 %>
%>
我的想法是既然是数据库操作类,所以类一实例化的时候就建立数据库连接,所以在使用的时候要先修改 Class_Initialize()中的代码,设置正确的数据库连接数据,设置好了就可以使用了。
1、一般的操作
   set myDb=new dbClass
myDb.sqlStr="select * from ..." '这里设置数据库操作语句
set rs=myDb.rsDB()
'这里返回rs,接着就按平时的写法写表现数据的代码
'.......
myDb.cRs(rs) '关闭释放rs
set myDb=nothing
myDb.sqlStr="select * from ..." '这里设置数据库操作语句
set rs=myDb.rsDB()
'这里返回rs,接着就按平时的写法写表现数据的代码
'.......
myDb.cRs(rs) '关闭释放rs
set myDb=nothing
若myDb.sqlStr设置的是更新、删除或添加的sql语句,那么就要用myDb.upDB()来执行,即
set myDb=new dbClass
myDb.sqlStr="update ..."
myDb.upDB()
set myDb=nothing
myDb.sqlStr="update ..."
myDb.upDB()
set myDb=nothing
 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号