CJJ专用ASP类库中的某个class
'----******************** tconnstring *****************************----
'数据库连接字符串结构体
class tconnstring
public dbname,dbpath,dbserver,dbuser,dbpass,dbtype
end class
'----******************** tconnstring *****************************----
'----********************* tdboperate *****************************----
'通用数据库操作类
class tdboperate
private cls_oconn,cls_ors '类私有connection对象、recordset对象
private cls_serrinfo,cls_sconn,cls_ssql,cls_surl,cls_sformaction
private cls_ipagesize '分页数
private cls_ltotalpage,cls_ltotalrecord,cls_lpageno
'类初始化
private sub class_initialize()
end sub
'*****************************************
' 类型: 属性
' 目的: 根据获取的connection string,创建数据库连接
' 输入: a_sconn:数据类型字符串
' 返回: 无
'*****************************************
public property let setconn(a_sconn)
dim sobjtype
sobjtype = lcase(typename(a_sconn))
if sobjtype <> "string" then
cls_serrinfo = cls_serrinfo & "<li>setconn:非法的字符串参数</li>" & chr(10)
exit property
end if
set cls_oconn = createobject("adodb.connection")
on error resume next
cls_oconn.open a_sconn
if err then
err.clear
set cls_oconn = nothing
on error goto 0
cls_serrinfo = cls_serrinfo & "<li>数据库连接出错</li>" & chr(10)
end if
on error goto 0
end property
'*****************************************
' 类型: 属性
' 目的: 根据获取的connection对象,创建数据库连接
' 输入: a_oconn:数据类型字符串
' 返回: 无
'*****************************************
public property set setconn(a_oconn)
dim sobjtype,sconn
dim oconnstr
sobjtype = lcase(typename(a_oconn))
select case sobjtype
case "connection"
'设置connection对象
set cls_oconn = a_oconn
case "tconnstring"
sconn = ""
set oconnstr = a_oconn
select case (oconnstr.dbtype)
case gbl_idb_access
sconn = "provider = micorsoft.jet.oledb.4.0; user id = " & oconnstr.dbuser & "; password = " & replace(oconnstr.dbpass, chr(0), "") & ";initial catalog = " & oconnstr.dbname & "; data source = " & sqllocalname & ";"
case gbl_idb_mssql
sconn = "provider = sqloledb; user id = " & oconnstr.dbuser & "; password = " & replace(oconnstr.dbpass, chr(0), "") & ";initial catalog = " & oconnstr.dbname & "; data source = " & oconnstr.dbserver & ";"
end select
if sconn = "" then
cls_serrinfo = cls_serrinfo & "<li>数据库连接对象出错,无法创建connection对象</li>" & chr(10)
exit property
end if
'设置connection连接串值,供connstr属性返回
cls_sconn = sconn
set cls_oconn = createobject("adodb.connection")
on error resume next
cls_oconn.open sconn
if err then
err.clear
set cls_oconn = nothing
cls_serrinfo = cls_serrinfo & "<li>数据库连接出错</li>" & chr(10)
end if
on error goto 0
case else
cls_serrinfo = cls_serrinfo & "<li>setconn:非法的对象参数</li>" & chr(10)
exit property
end select
end property
'*****************************************
' 类型: 属性
' 目的: 设置recordset对象
' 输入: a_ssql: sql语句。
' 返回: 无。
'*****************************************
public property let setrs(a_ssql)
if lcase(typename(cls_oconn)) <> "connection" then
cls_serrinfo = cls_serrinfo & "<li>非法的connection对象,无法创建recordset对象</li>" & chr(10)
exit property
end if
cls_ssql = a_ssql
'创建recordset对象
set cls_ors = createobject("adodb.recordset")
' on error resume next
cls_ors.open cls_ssql,cls_oconn,1,1
' on error goto 0
end property
'*****************************************
' 类型: 属性
' 目的: 设置recordset对象
' 输入: a_ors: recordset对象
' 返回: 无。
'*****************************************
public property set setrs(a_ors)
if lcase(typename(a_ors))<>"recordset" then
cls_serrinfo = cls_serrinfo & "<li>非法的recordset对象</li>" & chr(10)
exit property
end if
'设置recordset对象
set cls_ors = a_ors
end property
'*****************************************
' 类型: 属性
' 目的: 设置recordset对象
' 输入: a_ors: recordset对象
' 返回: 返回一recordset对象
'*****************************************
public property get getrs()
set getrs = cls_ors
end property
'获取connection对象
public property get getconn()
if cls_serrinfo <> "" then
call showerror()
end if
if lcase(typename(cls_oconn))<>"connection" then
cls_serrinfo = cls_serrinfo & "<li>connection对象获取失败</li>"
' exit property
end if
set getconn = cls_oconn
end property
'返回数据库连接字符串
public property get connstr
connstr = cls_sconn
end property
'设置第个页面显示的数据数
public property let pagesize(a_ipagesize)
if not isnumeric(a_ipagesize) then
cls_serrinfo = cls_serrinfo & "<li>无效的分页记录数参数</li>" & chr(10)
exit property
end if
cls_ipagesize = a_ipagesize
end property
'设置sql语句,用于建立recordset对象
public property let sql(a_ssql)
if isnone(a_ssql) then
cls_serrinfo = cls_serrinfo & "<li>没有设置sql,无法创建recordset对象</li>" & chr(10)
exit property
end if
cls_ssql = trim(a_ssql)
end property
'执行数据操作
public sub execute()
if cls_serrinfo <> "" then
showerror("<ul>" & chr(10) & cls_serrinfo & "</ul>" & chr(10))
exit sub
end if
if lcase(typename(cls_oconn))="connection" then
if isnumeric(cls_ipagesize) then
set cls_ors = createobject("adodb.recordset")
cls_ors.open cls_ssql,cls_oconn,1,1
else
end if
else
cls_serrinfo = cls_serrinfo & "<li>非法的connection对象</li>" & chr(10)
end if
end sub
'*****************************************
' 类型: 属性
' 目的: 设定或显示url。
' 输入: a_surl: 需要分页的文件地址。
' 返回: 无
'*****************************************
public property let url(byval a_surl)
cls_surl = a_surl
end property
'*****************************************
' 类型: 过程
' 目的: 统计总记录数、计算总页数
' 输入: 无
' 返回: 无
'*****************************************
private sub pagination(byval a_sstr)
dim iposition,cls_serrinfo,i,ors_temp,ltotalrecord
if cls_serrinfo <> "" then
call showerrors()
exit sub
end if
if cls_ors.eof and cls_ors.bof then
cls_serrinfo = cls_serrinfo & "<li>库中无任何记录</li>"
end if
'计算总计录数
select case lcase(typename(a_sstr))
case "string"
set ors_temp = cls_oconn.execute(a_sstr)
ltotalrecord = clng(ors_temp(0).value)
case "integer"
select case (int(trim(a_sstr)))
case gbl_ipagination_usercdcount '使的recordcount方法进行分页
ltotalrecord = cls_ors.recordcount
case gbl_ipagination_usepgcount '使用pagecount方法进行分页
ltotalrecord = cls_ors.pagecount * cls_ipagesize
end select
end select
cls_ltotalrecord = ltotalrecord
if (cls_ltotalrecord<=2147483647 and cls_ltotalrecord>=-2147483648) then
cls_ltotalrecord = clng(cls_ltotalrecord)
else
cls_ltotalrecord = 2147483647
end if
if cls_ltotalrecord <0 then
cls_ltotalrecord = 0
end if
'计算总页数
if cls_ltotalrecord mod cls_ipagesize = 0 then
cls_ltotalpage = clng(cls_ltotalrecord \ cls_ipagesize * -1)*-1
else
cls_ltotalpage = clng(cls_ltotalrecord \ cls_ipagesize * -1)*-1 + 1
end if
'获取当前页参数
cls_lpageno = trim(request.querystring("page"))
if cls_lpageno = "" then
cls_lpageno = trim(request.form("page"))
if cls_lpageno = "" then
cls_lpageno = 1
end if
end if
'如果没有选择第几页,则默认显示第一页
if cls_lpageno <> "" and isnumeric(cls_lpageno) then
if (cls_lpageno <= 2147483647 and cls_lpageno>=-2147483648) then
cls_lpageno = clng(cls_lpageno)
else
cls_lpageno = 2147483647
end if
if (cls_lpageno<=0) then
cls_lpageno = 1
end if
else '当前页参数为空或者非数字,默认将转到第1页
cls_lpageno=1
end if
if (cls_lpageno > cls_ltotalpage and cls_ltotalpage<>0) then
cls_lpageno = cls_ltotalpage
end if
cls_ors.pagesize = cls_ipagesize
cls_ors.absolutepage = cls_lpageno
iposition = instrrev(cls_surl,"?")
cls_sformaction = cls_surl
if iposition > 0 then
cls_surl = cls_surl & "&page="
else
cls_surl = cls_surl & "?page="
end if
end sub
'*****************************************
' 类型: 过程
' 目的: 显示分页信息
' 输入: 无
' 返回: 无
'*****************************************
public sub pages(byval a_sstr)
dim strpages,k,inttemp,inttemp1
dim sresult
if not isnone(cls_serrinfo) then
call showerrors()
end if
'计算总页数及总记录数
call pagination(a_sstr)
if cls_ltotalpage = 1 then exit sub
sresult = sresult & "<table class=""clsshowpage"">" & chr(10)
sresult = sresult & " <tr>" & chr(10) & " <td>" & chr(10)
sresult = sresult & " <table width=""100%"">" & chr(10)
sresult = sresult & " <tr>" & chr(10) & " <td class=""pagetext"">" & chr(10)
if cls_ltotalpage >= 1 then
if cls_lpageno <= 1 then
sresult = sresult & "首页 前页 <a href=""" & cls_surl & cls_lpageno+1 & """>后页</a> <a href=""" & cls_surl & cls_ltotalpage & """>末页</a>" & chr(10)
else
if cls_lpageno >= cls_ltotalpage then
sresult = sresult & "<a href=""" & cls_surl & "1"">首页</a> <a href=""" & cls_surl & cls_lpageno -1 & """>前页</a> " & "后页 末页" & chr(10)
else
sresult = sresult & "<a href=""" & cls_surl & "1"">首页</a> <a href=""" & cls_surl & cls_lpageno -1 & """>前页</a> " & "<a href=""" & cls_surl & cls_lpageno+1 & """>后页</a> <a href=""" & cls_surl & cls_ltotalpage & """>末页</a>" & chr(10)
end if
end if
sresult = sresult & " 页次:<strong>" & cls_lpageno & "</strong>/" & cls_ltotalpage & "页 共<strong>" & cls_ltotalrecord & "</strong>条记录 <strong>" & cls_ipagesize & "</strong>条/页</td>" & chr(10)
sresult = sresult & " <form name=""gopage"" action=""" & cls_sformaction & """ method=""post"">" & chr(10)
sresult = sresult & " <td> 第"
sresult = sresult & " <input type=""text"" name=""pageno"" class=""inputpage"" title=""请输入页号,然后回车"">页 " & chr(10)
sresult = sresult & "<input type=""submit"" class=""gotopage"" value=""go""></td></form></tr>" & chr(10)
end if
sresult = sresult & " </table>" & chr(10) & " </td>" & chr(10) & " </tr>" & chr(10) & "</table>" & chr(10)
'输出分页信息
response.write("result:" & sresult)
end sub
'类销毁
private sub class_terminate()
if lcase(typename(cls_oconn))<>"nothing" then
cls_oconn.close
set cls_oconn = nothing
end if
if lcase(typename(cls_ors))<>"nothing" then
' cls_ors.close
set cls_ors = nothing
end if
end sub
'*****************************************
' 类型: 过程
' 目的: 显示分页类中出现的错误信息
' 输入: 无
' 返回: 无
'*****************************************
private sub showerrors()
if cls_cls_serrinfo <> "" then
cls_cls_serrinfo = "<ul>" & chr(10) & cls_serrinfo & "</ul>" & chr(10)
response.write(cls_cls_serrinfo)
response.end
end if
end sub
end class
'----********************* tdboperate *****************************----
稍提一个编码风格
个人觉得代码混排是个鸡肋,混排的可读性差,所以我一般都只是少量混排,尽量将代码和html分离。
<!--#include file="pubdb.asp"-->
<%
'*****************************************
'类型:函数
'目的:报错
'参数:
'a_num:报错信息参数
'*****************************************
private function showerror(a_num)
dim serrinfo
serrinfo = ""
response.write("<p>error number:era_" & a_num & "</p>")
select case a_num
case "1000"
serrinfo = "参数类型不正确,请检查"
case "1100"
serrinfo = "无法打开数据库连接"
case else
serrinfo = "发现未知错误,请与管理员联系"
end select
serrinfo = "<p>error description:" & serrinfo & "</p>"
response.write(serrinfo)
response.end
end function
dim ors,shtml
call opendb()
set ors = oconn.openschema(20)
shtml=""
ors.movefirst
'循环读取数据库中的表名
do while not ors.eof
if ucase(ors(3))="table" then
stemp = trim(ors(2))
if stbname = stemp then
shtml= shtml & space(2) & "<option value=""" & stemp & """ selected=""selected"">" & stemp & "</option>" & chr(10)
else
shtml= shtml & space(2) & "<option value=""" & stemp & """>" & stemp & "</option>" & chr(10)
end if
end if
ors.movenext
loop
call closedb()
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>数据库名</title>
<script type="text/javascript">
<!--
var sdstid="";
function serverresult(a_surl,a_ssrcid,a_sdstid) {
var svalue = document.getelementbyid(a_ssrcid).value;
sdstid=a_sdstid;
if ((svalue == null) || (svalue == "")) return;
var surl = a_surl+svalue;
oxml.open("get", surl,true);
oxml.onreadystatechange = updatedata;
oxml.send(null);
}
function updatedata() {
var aelmnts = sdstid.split(',');
var oelmnt = null;
var atags=null;
var oxmldata=null;
if ((oxml.readystate == 4) && (oxml.status == 200)) {
atags = oxml.responsexml.documentelement.getelementsbytagname("cjjitem");
if (atags.length!=aelmnts.length) {
alert('获取的服务器端的数据错误!');
return null;
}
for (var i=0;i<aelmnts.length ;i++ ) {
oelmnt = document.getelementbyid(aelmnts[i]);
oelmnt.innerhtml=atags[i].firstchild.data;
}
}
return true;
}
var oxml = false;
if (window.activexobject) {
oxml = new activexobject("microsoft.xmlhttp");
} else if (window.xmlhttprequest) {
oxml=new xmlhttprequest();
}
//-->
</script>
</head>
<body>
<form method="post" name="form1" action="addformdata.asp">
<p> </p>
<p>数据库名:<input type="text" name="txtdbname" size="7" value="work"> 数据库用户名:<input type="text" name="dbusername" size="8" value="sa">数据库密码:<input type="password" name="dbuserpassword" size="10" value=""> 数据库服务器路径:<input type="text" name="dbserverpath" size="20" value="127.0.0.1"></p>
<p>数据表名:<select size="1" id="slttbname" name="slttbname" onchange="serverresult('getfieldlist.asp?n=','slttbname','fieldcount,tblfields');">
<option selected="selected">请选择一个表</option>
<%=shtml%>
</select></p>
<div id="fieldcount">表字段个数:<input type="text" id="txtfldcount" name="txtfldcount" value="0" /></div>
<table id="tblfields" border="1" width="91%">
<thead>
<tr>
<td align="center" width="94">字段名</td>
<td align="center" width="113">字段类型</td>
<td width="27" align="center">使用</td>
<td width="18" align="center">只读</td>
<td align="center" width="80">表单项类型</td>
<td align="center" width="100">表单项名称</td>
<td align="center" width="92">表单项描述</td>
<td align="center" width="87">表单项验证</td>
<td align="center">表单项默认值</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
<p align="left">需要生成的动态asp网页类型:<select size="1" name="sltasptype">
<option value="0">数据添加</option>
<option value="1">数据编辑</option>
<option value="2">数据删除</option>
<option value="3">数据管理</option>
<option value="4">数据列表</option>
</select> 文件名:<input type="text" name="txtfilename" size="17" value="">
文件类型:<select size="1" name="sltfiletype">
<option value="asp">asp</option>
<option value="php">php</option>
<option value="jsp">jsp</option>
<option value="perl">perl</option>
<option value="vb.net">vb.net</option>
<option value="c#">c#</option>
</select>
<input type="submit" value="生成文件" name="action"></p>
</form>
</body>
</html>