asp_数据库操作封装
来源:asp之家 时间:2010-04-03 21:00:00
一、conn.asp
<%
'@Language="VBSCRIPT" CODEPAGE=65001
'meta http-equiv="Content-Type" content="text/html; charset=gb2312"
' 936 utf-8
class TDB
function qry(byval sqlquery)
dim adoConn,adoRS
set adoConn=server.createobject("ADODB.connection")
adoConn.open "provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath(".") & "/sys.mdb"
set adoRS=server.createobject("ADODB.RecordSet")
adoRS.open sqlquery,adoConn,1,3 'support movelast (adOpenKeySet,adLockOptimistic)
'set adoRS=adoConn.execute(sqlquery) 'don't support movelast
'
dim rtnstr
rtnstr="<?xml version='1.0' encoding='gb2312' standalone='yes' ?>" & chr(13)& chr(10)
rtnstr=rtnstr & "<rs>" & chr(13)& chr(10)
irow=0
adoRS.movefirst
do while not adoRS.Eof
rtnstr=rtnstr & " <r" & cstr(irow) & ">" & chr(13)& chr(10)
for i=0 to adoRS.Fields.Count-1
rtnstr=rtnstr & " <" & adoRS.Fields(i).name & ">" & chr(13)& chr(10)
rtnstr=rtnstr & "<![CDATA[ " & adoRS.Fields(i) & " ]]>" & chr(13)& chr(10)
rtnstr=rtnstr & " </" & adoRS.Fields(i).name & ">" & chr(13)& chr(10)
next
rtnstr=rtnstr & " </r" & cstr(irow) & ">" & chr(13)& chr(10)
irow=irow+1
adoRS.movenext
loop
rtnstr=rtnstr & "</rs>"
'
adoRS.Close
adoConn.Close
set adoRS=nothing
set adoConn=nothing
qry=rtnstr
end function
function qrybypage_xml(byval sqlquery,byval ipage,byval ipagesize)
dim adoConn,adoRS,iCounts,iPagecounts,iStartPos,iEndPos
set adoConn=server.createobject("ADODB.connection")
adoConn.open "provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath(".") & "/sys.mdb"
set adoRS=server.createobject("ADODB.RecordSet")
adoRS.open sqlquery,adoConn,1,3 'support movelast (adOpenKeySet,adLockOptimistic)
'check cur page
if ipage<=0 then ipage=1
adoRS.movelast
iCounts=adoRS.RecordCount
iPagecounts = (iCounts - 1) \ ipagesize + 1 'page counts, good job! vb all div=\ not /
if ipage>iPagecounts then ipage=iPagecounts
'get start pos,end pos
iStartPos = (ipage - 1) * ipagesize
iEndPos = iStartPos + ipagesize-1
if (iEndPos > iCounts-1) then iEndPos = iCounts-1 'last page no full page
'
dim rtnstr
rtnstr="<?xml version='1.0' encoding='gb2312' standalone='yes' ?>" & chr(13)& chr(10)
rtnstr=rtnstr & "<pageinfo>" & chr(13)& chr(10)
rtnstr=rtnstr & " <counts>" & cstr(iCounts) & "</counts>" & chr(13)& chr(10)
rtnstr=rtnstr & " <pages>" & cstr(iPagecounts) & "</pages>" & chr(13)& chr(10)
rtnstr=rtnstr & " <page>" & cstr(ipage) & "</page>" & chr(13)& chr(10)
rtnstr=rtnstr & " <pagesize>" & cstr(ipagesize) & "</pagesize>" & chr(13)& chr(10)
rtnstr=rtnstr & "</pageinfo>" & chr(13)& chr(10)
'
rtnstr=rtnstr & "<rs>" & chr(13)& chr(10)
irow=0
adoRS.movefirst
do while not adoRS.Eof
if irow>=iStartPos and irow<=iEndPos then
rtnstr=rtnstr & " <r" & cstr(irow) & ">" & chr(13)& chr(10)
for i=0 to adoRS.Fields.Count-1
rtnstr=rtnstr & " <" & adoRS.Fields(i).name & ">" & chr(13)& chr(10)
rtnstr=rtnstr & "<![CDATA[ " & adoRS.Fields(i) & " ]]>" & chr(13)& chr(10)
rtnstr=rtnstr & " </" & adoRS.Fields(i).name & ">" & chr(13)& chr(10)
next
rtnstr=rtnstr & " </r" & cstr(irow) & ">" & chr(13)& chr(10)
end if
if irow>iEndPos then exit do
irow=irow+1
adoRS.movenext
loop
rtnstr=rtnstr & "</rs>"
'
adoRS.Close
adoConn.Close
set adoRS=nothing
set adoConn=nothing
qrybypage_xml=rtnstr
end function
function qrybypage_array(byval sqlquery,byval ipage,byval ipagesize)
dim adoConn,adoRS,iCounts,iPagecounts,iStartPos,iEndPos
set adoConn=server.createobject("ADODB.connection")
adoConn.open "provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath(".") & "/sys.mdb"
set adoRS=server.createobject("ADODB.RecordSet")
adoRS.open sqlquery,adoConn,1,3 'support movelast (adOpenKeySet,adLockOptimistic)
'check cur page
if ipage<=0 then ipage=1
adoRS.movelast
iCounts=adoRS.RecordCount
iPagecounts = (iCounts - 1) \ ipagesize + 1 'page counts, good job! vb int div=\ not /
if ipage>iPagecounts then ipage=iPagecounts
'get start pos,end pos
iStartPos = (ipage - 1) * ipagesize
iEndPos = iStartPos + ipagesize-1
if (iEndPos > iCounts-1) then iEndPos = iCounts-1 'last page no full page
'
dim rtnstr
rtnstr="@data@" & cstr(iCounts) & "," & cstr(iPagecounts) & "," & cstr(ipage) & "," & cstr(ipagesize)
rtnstr=rtnstr & chr(13) & chr(13)
for i=0 to adoRS.Fields.Count-1
if i=adoRS.Fields.Count-1 then
rtnstr=rtnstr & adoRS.Fields(i).name
else
rtnstr=rtnstr & adoRS.Fields(i).name & ","
end if
next
'
irow=0
adoRS.movefirst
dim tmpstr
do while not adoRS.Eof
if irow>=iStartPos and irow<=iEndPos then
tmpstr=""
for i=0 to adoRS.Fields.Count-1
if i=adoRS.Fields.Count-1 then
tmpstr=tmpstr & adoRS.Fields(i)
else
tmpstr=tmpstr & adoRS.Fields(i) & chr(10) & chr(10)
end if
next
rtnstr=rtnstr & chr(13) & chr(13) & tmpstr
end if
if irow>iEndPos then exit do
irow=irow+1
adoRS.movenext
loop
'
adoRS.Close
adoConn.Close
set adoRS=nothing
set adoConn=nothing
qrybypage_array=rtnstr
end function
function cmd(byval sqlcmd)
dim adoConn
set adoConn=server.createobject("ADODB.connection")
adoConn.open "provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath(".") & "/sys.mdb"
adoConn.execute(sqlcmd)
adoConn.Close
set adoConn=nothing
end function
end class
function getxmldata(byval xmlstr,byval row,byval key)
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.Async=False
xml.ValidateOnParse=False
xml.LoadXML(xmlstr) 'Load()
sRtn=""
If xml.ReadyState>2 Then
Set StartNode=xml.getElementsByTagName(key)
if row<=(StartNode.Length-1) then
sRtn=StartNode.Item(row).Text
else
sRtn="" '"no data:" & cstr(StartNode.Length) & xmlstr
end if
'For i=0 To (StartNode.Length-1)
'sRtn=StartNode.Item(i).getElementsByTagName(key)
'Next
Else
sRtn="" '"no ready" & xmlstr
End If
Set xml=Nothing
getxmldata=sRtn
end function
%>
二、调用示例
index.asp
<!-- #include file="conn.asp" -->
<%
dim db
set db=new TDB
'xmlstr=db.qry("select * from xms_hotph")
'response.write("xml in source code")
'response.write(xmlstr)
'response.write("*******************")
dim ipage,ipagesize
ipage=cint(request.queryString("page"))
ipagesize=cint(request.queryString("pagesize"))
'xmlstr=db.qrybypage_xml("select * from xms_hotph",ipage,ipagesize)
'response.write(xmlstr)
xmlstr=db.qrybypage_array("select * from xms_hotph",ipage,ipagesize)
response.write(xmlstr)
'response.write(getxmldata(xmlstr,0,"phnumber"))
'db.cmd("insert into xms_hotph (type,phnumber) values (99,'aaa')")
%>