欢迎光临
我们一直在努力

我在开发过程总结的一套实现常用功能的函数

建站超值云服务器,限时71元/月

<%

option explicit

===================================================

public function mynownumber()

mynownumber=year(now)& month(now) & day(now) & hour(now) & minute(now) & second(now)

end function

public function sdatagrid(sqlstr,connstr,pagesize,pagenum,beginfield,endfield,idfield,httpstr,pageinfo)以表格形式显示数据

这个函数本打算用来实现点击表头排序功能,可是后来总是随机性无故出错,所以就没在现用了,哪们高手可以看看

datagrid功能:

将数据以表格形式显出来,

根据需要可能确定显示的字段,

页号,每页显示的记录数

复选框绑定的字段

修改数据时所连接到的设定的网页,传递的参数名是idfield

调用实例 call datagrid("select * from mater_bcode","driver={sql server};uid=sa;pwd=passed;database=cthpdb;server=scb-web",10,20,2,50,0,"http://www.clkhome/mater_code1/tools/aaa.asp","null")

参数说明

sqlstr: 将要查询的sql语句

connstr: 数据库连接字符串

pagesize: 数据集每页的记录数,pagesize="max"时不分页

pagenum: 数据集中将要显示的页号

beginfield:在记录集中开始显示的字段位置

endfield: 在记录集中结束显示的字段位置

idfield: 用于给复选框的value赋值的字段在记录集中的位置,可以用于提交到其它页,其它页做处理的依据

如果idfield<0 or idfield> rs.fields.count或不是数字 则不显示复选框

httpstr 修改记录时连接到的网页,传递的是idfield的值,如果 httpstr="0"则不显示修改连接

pageinfo 确定是否显示"第1页,共1页"的提示,pageinfo="null"时不显示

返回值, 返回的是记录集的当前页号

on error resume next

dim conn,rs

if isnumeric(pagesize)then rs.pagesize=pagesize

set conn=server.createobject ("adodb.connection")

set rs=server.createobject ("adodb.recordset")

conn.open connstr

rs.open sqlstr,conn,1,3

if err.number<>0 then

response.write writeinfo("<br>datagrid函数在运行出现了错误!<br>错描述:" & err.description & "<br>")

datagrid="err"

exit function

end if

if rs.recordcount<1 then

response.write "<font size=-1 color=#ff0000>&nbsp;没有数据…</font>"

datagrid=0

exit function

end if

————对参数据进行处理———————-

———-确定如何显示字段—————–

if not isnumeric(beginfield) or beginfield<0 then

beginfield=0确保开始显示的位置在合理范围内

else

if beginfield>rs.fields.count-1 then beginfield=rs.fields.count-1保证到少显示一个字段

if beginfield<0 then beginfield=0

end if

if not isnumeric(endfield) then endfield=rs.fields.count-1

if endfield>rs.fields.count-1 then endfield=rs.fields.count-1

if endfield<=beginfield or endfield<0 then endfield=beginfield保证到少显示一个字段

———-复选框、修改链接的处理在成生表格时同步完成————–

———分页处理——————

if ucase(trim(pagesize))="max" then

pagesize=rs.recordcount

rs.pagesize=rs.recordcount

pagenum=1

else

if not isnumeric(pagesize) or pagesize<1 then pagesize=10

if pagesize>rs.recordcount then pagesize=rs.recordcount

rs.pagesize=pagesize

———页号处理—————-

if trim(ucase(pagenum))="max" then pagenum=rs.pagecount

if not isnumeric(pagenum) then pagenum=1

if pagenum<1 then pagenum=1

if cint(pagenum-rs.pagecount)>0 then pagenum=rs.pagecount

end if

rs.absolutepage=pagenum

if err.number<>0 then

dbinfo="数据库连接错误"

datagrid=0

exit function

else

if rs.recordcount<1 then

response.write "没有找到记录"

datagrid=0

exit function

else

if ucase(trim(pageinfo))<>"null" then 确定是显示页号信息

response.write "<font size=-1 color=#666666>第<font color=#ff0000>" & pagenum & "</font>页,共<font color=#ff0000>" & rs.pagecount & "</font>页"

end if

写入表头

response.write "<table width=100% border=0 cellspacing=1 cellpadding=0 bgcolor=#999999>"

response.write "<tr bgcolor=#cccccc>"

response.write "<td width=1% align=center><b><font color=#666666 size=-1>序</font></b></td>"

dim i

for i=beginfield to endfield

response.write "<td align=center><b><font color=#666666 size=-1>" & rs.fields(i).name & "</font></b></td>"

next

if isnumeric(idfield) then 复选框及修改链接处理

if idfield>=0 and (idfield – rs.fields.count<=0) then

response.write "<td width=1% align=center><b><font color=#666666 size=-1>删</font></b></td>"

if trim(httpstr)<>"0" then response.write "<td width=1%><b><font color=#666666 size=-1>修</font></b></td>"

end if

end if

response.write "</tr>"

写入字段信息

dim recordnum,fieldnum

for recordnum=0 to pagesize-1 ?????????????????

response.write "<tr bgcolor=#ffffff>"

response.write "<td><font size=-1>" & recordnum +1+(pagenum-1)*pagesize & "</font></td>"

for fieldnum=beginfield to endfield 写入字段值

response.write "<input type=text name=textfield value=" & trim(rs.fields(fieldnum).value) & ">"

if trim(rs.fields(fieldnum).value)="" or isnull(rs.fields(fieldnum).value) then

response.write "<td>&nbsp;</td>"

else

response.write "<td><font size=-1><input type=text name=textfield style= border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px value=" & trim(rs.fields(fieldnum).value) & "></font></td>"

end if

next

if isnumeric(idfield) then 复选框处理

if idfield>=0 or (idfield – rs.fields.count<=0) then

response.write "<td><input type=checkbox name=idfield value="& rs.fields(idfield).value &"></td>"

if trim(httpstr)<>"0" then response.write "<td><font size=-1><a href=" & httpstr & "?idfield="& rs.fields(idfield).value &">改</a></font></td>"

end if

end if

response.write "</tr>"

rs.movenext

if rs.eof then exit for 最后不到一整页时,也跳出

next

response.write "</table>"

end if

end if

rs.close

conn.close

set rs=nothing

set conn=nothing

err.clear

datagrid=pagenum

end function

public function datagrid(sqlstr,connstr,pagesize,pagenum,beginfield,endfield,idfield,httpstr,pageinfo)以表格形式显示数据

datagrid功能:

将数据以表格形式显出来,

根据需要可能确定显示的字段,

页号,每页显示的记录数

复选框绑定的字段

修改数据时所连接到的设定的网页,传递的参数名是idfield

调用实例 call datagrid("select * from mater_bcode","driver={sql server};uid=sa;pwd=passed;database=cthpdb;server=scb-web",10,20,2,50,0,"http://www.clkhome/mater_code1/tools/aaa.asp","null")

参数说明

sqlstr: 将要查询的sql语句

connstr: 数据库连接字符串

pagesize: 数据集每页的记录数,pagesize="max"时不分页

pagenum: 数据集中将要显示的页号

beginfield:在记录集中开始显示的字段位置

endfield: 在记录集中结束显示的字段位置

idfield: 用于给复选框的value赋值的字段在记录集中的位置,可以用于提交到其它页,其它页做处理的依据

如果idfield<0 or idfield> rs.fields.count或不是数字 则不显示复选框

httpstr 修改记录时连接到的网页,传递的是idfield的值,如果 httpstr="0"则不显示修改连接

pageinfo 确定是否显示"第1页,共1页"的提示,pageinfo="null"时不显示

返回值, 返回的是记录集的当前页号

on error resume next

dim conn,rs

if isnumeric(pagesize)then rs.pagesize=pagesize

set conn=server.createobject ("adodb.connection")

set rs=server.createobject ("adodb.recordset")

conn.open connstr

rs.open sqlstr,conn,1,3

if err.number<>0 then

response.write writeinfo("<br>datagrid函数在运行出现了错误!<br>错描述:" & err.description & "<br>")

datagrid="err"

exit function

end if

if rs.recordcount<1 then

response.write "<font size=-1 color=#ff0000>&nbsp;没有数据…</font>"

datagrid=0

exit function

end if

————对参数据进行处理———————-

———-确定如何显示字段—————–

if not isnumeric(beginfield) or beginfield<0 then

beginfield=0确保开始显示的位置在合理范围内

else

if beginfield>rs.fields.count-1 then beginfield=rs.fields.count-1保证到少显示一个字段

if beginfield<0 then beginfield=0

end if

if not isnumeric(endfield) then endfield=rs.fields.count-1

if endfield>rs.fields.count-1 then endfield=rs.fields.count-1

if endfield<=beginfield or endfield<0 then endfield=beginfield保证到少显示一个字段

———-复选框、修改链接的处理在成生表格时同步完成————–

———分页处理——————

if ucase(trim(pagesize))="max" then

pagesize=rs.recordcount

rs.pagesize=rs.recordcount

pagenum=1

else

if not isnumeric(pagesize) or pagesize<1 then pagesize=10

if pagesize>rs.recordcount then pagesize=rs.recordcount

rs.pagesize=pagesize

———页号处理—————-

if trim(ucase(pagenum))="max" then pagenum=rs.pagecount

if not isnumeric(pagenum) then pagenum=1

if pagenum<1 then pagenum=1

if cint(pagenum-rs.pagecount)>0 then pagenum=rs.pagecount

end if

rs.absolutepage=pagenum

if err.number<>0 then

dbinfo="数据库连接错误"

datagrid=0

exit function

else

if rs.recordcount<1 then

response.write "没有找到记录"

datagrid=0

exit function

else

if ucase(trim(pageinfo))<>"null" then 确定是显示页号信息

response.write "<font size=-1 color=#666666>第<font color=#ff0000>" & pagenum & "</font>页,共<font color=#ff0000>" & rs.pagecount & "</font>页"

end if

写入表头

response.write "<table width=100% border=0 cellspacing=1 cellpadding=0 bgcolor=#999999>"

response.write "<tr bgcolor=#cccccc>"

response.write "<td width=1% align=center onclick=form1.submit()><b><font color=#666666 size=-1>序</font></b></td>"

dim i

for i=beginfield to endfield

response.write "<td align=center><b><font color=#666666 size=-1>" & rs.fields(i).name & "</font></b></td>"

next

if isnumeric(idfield) then 复选框及修改链接处理

if idfield>=0 and (idfield – rs.fields.count<=0) then

response.write "<td width=1% align=center><b><font color=#666666 size=-1>删</font></b></td>"

if trim(httpstr)<>"0" then response.write "<td width=1%><b><font color=#666666 size=-1>修</font></b></td>"

end if

end if

response.write "</tr>"

写入字段信息

dim recordnum,fieldnum

for recordnum=0 to pagesize-1 ?????????????????

response.write "<tr bgcolor=#ffffff>"

response.write "<td><font size=-1>" & recordnum +1+(pagenum-1)*pagesize & "</font></td>"

for fieldnum=beginfield to endfield 写入字段值

if trim(rs.fields(fieldnum).value)="" or isnull(rs.fields(fieldnum).value) then

response.write "<td>&nbsp;</td>"

else

response.write "<td><font size=-1>&nbsp;" & trim(rs.fields(fieldnum).value) & "</font></td>"

end if

next

if isnumeric(idfield) then 复选框处理

if idfield>=0 or (idfield – rs.fields.count<=0) then

response.write "<td><input type=checkbox name=idfield value="& rs.fields(idfield).value &"></td>"

if trim(httpstr)<>"0" then response.write "<td><font size=-1><a href=" & httpstr & "?idfield="& rs.fields(idfield).value &">改</a></font></td>"

end if

end if

response.write "</tr>"

rs.movenext

if rs.eof then exit for 最后不到一整页时,也跳出

next

response.write "</table>"

end if

end if

rs.close

conn.close

set rs=nothing

set conn=nothing

err.clear

datagrid=pagenum

end function

=========================================================================================================================================================

public function doubledatagrid(sqlstr,connstr,pagesize,pagenum,beginfield,endfield,idfield,httpstr,ssqlstr,sbeginfield,sendfield,relationfieldstr,foreignfieldnum,addwhere,orderbystr)主从表格式显示数据

doubledatagrid功能:

将数据以表格形式显出来,

根据需要可能确定显示的字段,

页号,每页显示的记录数

复选框绑定的字段

修改数据时所连接到的设定的网页,传递的参数名是idfield

调用实例 page=doubledatagrid("select * from mater_bcode","dsn=clkdb;uid=sa;pwd=passed",10,20,2,50,"null","http://www.clkhome/mater_code1/tools/aaa.asp","select price as 价格, num as 数量, materdate as 日期 from mater_price",0,"max","mater_id",0,"y","null")

参数说明

sqlstr: 将要查询的sql语句

connstr: 数据库连接字符串

pagesize: 数据集每页的记录数

pagenum: 数据集中将要显示的页号

beginfield:在记录集中开始显示的字段位置

endfield: 在记录集中结束显示的字段位置

idfield: 用于给复选框的value赋值的字段在记录集中的位置,可以用于提交到其它页,其它页做处理的依据

如果idfield<0 or idfield> rs.fields.count或不是数字 则不显示复选框

httpstr 修改记录时连接到的网页,传递的是idfield的值,如果 httpstr="0"则不显示修改连接

ssqlstr: 从表的sql语句,不能句括order by 子句,因为要通过类似于 "where 子表.字段=主表.字段"的方式将两个表联系起来,

而where必须在ordey by子句这前使用,才能符合sql语法

sbeginfield: 从表中在记录集中开始显示的字段位置

sendfield: 从表中在记录集中结束显示的字段位置

relationfieldstr: 从表中,与主表的关联的字段名,使用方式如 where relationfieldstr= abc

foreignfieldnum: 主表中,与从表关联的字段在主表记录是中的位置,

之所以用以位置(index)而不直接写确定的值,是因为当主表的记录集movenext后,相应的值要变以

生成对应的从表记录集,使用方式如 where relationfieldstr= rs(foreignfieldnum)

addwhere: 确定将ssqlstr与生成的关联字符串("where 子表.字段=主表.字段")连接时是用 "where "还是"and"

addwhere<>"null"时用"where" addwhere="null"时用"and"

orderbystr: 从表的sql语句的orderbystr子句

返回值, 返回的是记录集的当前页号

on error resume next

dim conn,rs

set conn=server.createobject ("adodb.connection")

set rs=server.createobject ("adodb.recordset")

conn.open connstr

rs.open sqlstr,conn,1,3

if err.number <>0 then

response.write "<br>doubledatagrid函数出错错误:<br>" &err.description

exit function

end if

if rs.recordcount<1 then

response.write "<font size=-1 color=#ff0000>&nbsp;没有数据…</font>"

doubledatagrid=0

exit function

end if

————对参数据进行处理———————-

———-确定如何显示字段—————–

if not isnumeric(beginfield) or beginfield<0 then

beginfield=0确保开始显示的位置在合理范围内

else

if beginfield>rs.fields.count-2 then beginfield=rs.fields.count-2保证到少显示一个字段

if beginfield<0 then beginfield=0

end if

if not isnumeric(endfield) then endfield=rs.fields.count-1

if endfield>rs.fields.count-1 then endfield=rs.fields.count-1

if endfield<=beginfield or endfield<0 then endfield=beginfield-1保证到少显示一个字段

———-复选框、修改链接的处理在成生表格时同步完成————–

———分页处理——————

if ucase(trim(pagesize))="max" then

pagesize=rs.recordcount

rs.pagesize=rs.recordcount

pagenum=1

else

if not isnumeric(pagesize) or pagesize<1 then pagesize=10

if pagesize>rs.recordcount then pagesize=rs.recordcount

rs.pagesize=pagesize

———页号处理—————-

if trim(ucase(pagenum))="max" then pagenum=rs.pagecount

if not isnumeric(pagenum) then pagenum=1

if pagenum<1 then pagenum=1

if cint(pagenum-rs.pagecount)>0 then pagenum=rs.pagecount

end if

rs.absolutepage=pagenum

if err.number<>0 then

dbinfo="数据库连接错误"

doubledatagrid=0

exit function

else

if rs.recordcount<1 then

response.write "没有找到记录"

doubledatagrid=0

exit function

else

response.write "<font size=-1 color=#666666>第<font color=#ff0000>" & pagenum & "</font>页,共<font color=#ff0000>" & rs.pagecount & "</font>页"

写入表头

response.write "<table width=100% border=0 cellspacing=1 cellpadding=0 bgcolor=#999999>"

response.write "<tr bgcolor=#cccccc>"

response.write "<td width=1% align=center><b><font color=#666666 size=-1>序</font></b></td>"

dim i

for i=beginfield to endfield

response.write "<td align=center><b><font color=#666666 size=-1>" & rs.fields(i).name & "</font></b></td>"

next

if isnumeric(idfield) then 复选框及修改链接处理

if idfield>=0 and (idfield – rs.fields.count<=0) then

response.write "<td width=1%align=center><b><font color=#666666 size=-1>删</font></b></td>"

if trim(httpstr)<>"0" then response.write "<td width=1%><b><font color=#666666 size=-1>修</font></b></td>"

end if

end if

response.write "</tr>"

写入字段信息

dim recordnum,fieldnum,swhere,sssql

for recordnum=0 to pagesize-1 ?????????????????

response.write "<tr bgcolor=#ffffff>"

response.write "<td><font size=-1>" & recordnum +1+(pagenum-1)*pagesize & "</font></td>"

for fieldnum=beginfield to endfield 写入字段值

if trim(rs.fields(fieldnum).value)="" or isnull(rs.fields(fieldnum).value) then

response.write "<td>&nbsp;</td>"

else

response.write "<td><font size=-1>&nbsp;" & trim(rs.fields(fieldnum).value) & "</font></td>"

end if

next

if isnumeric(idfield) then 复选框处理

if idfield>=0 or (idfield – rs.fields.count<=0) then

response.write "<td><input type=checkbox name=idfield value="& rs.fields(idfield).value &"></td>"

if trim(httpstr)<>"0" then response.write "<td><font size=-1><a href=" & httpstr & "?idfield="& rs.fields(idfield).value &">改</a></font></td>"

end if

end if

response.write "</tr>"

—-写入从表

response.write "<tr bgcolor=#ffffff>"

response.write "<td colspan=2>&nbsp;"

response.write "</td>"

保证从表的长度比主表少一格

response.write "<td colspan=" & rs.fields.count-beginfield-1 & ">"

swhere=""每次生成新的从表的sql语句新清空临时变量

确定主表的记录集中与从表相关的字段位置,生成开相应的关联字符串

if not isnumeric(foreignfieldnum) then foreignfieldnum=0

if foreignfieldnum<0 or foreignfieldnum>rs.fields.count-1 then foreignfieldnum=0

swhere=" " & relationfieldstr & "=" & rs(foreignfieldnum).value &"" 对于sql_server最好用这句进行连接,这一句更灵活,可对用于字符型和数值型,(日期型没试过)

swhere=" " & relationfieldstr & "=" & rs(foreignfieldnum).value专门应用于access,因为是对于access来说数值型字段不能加"",这句只对数值型的字段才有效

if ucase(trim(addwhere)) <> "null" then

swhere=" where " & swhere

else

swhere=" and " & swhere

end if

sssql=ssqlstr & swhere

if ucase(trim(orderbystr))<>"null" then sssqlstr=ssqlstr & orderbystr

call datagrid(sssql,connstr,"max",1,sbeginfield,sendfield,"null","null","null")

response.write "</td>"

response.write "</tr>"

rs.movenext

if rs.eof then exit for 最后不到一整页时,也跳出

next

response.write "</table>"

end if

end if

rs.close

conn.close

set rs=nothing

set conn=nothing

err.clear

doubledatagrid=pagenum

end function

=========================================================================================================================================================

public function inputform(sqlstr,connstr,yesadd,beginfield,endfield,idfield,coridstr,corsize)

inputform功能:根据connstr和sqlstr生成数据输入表单,可用于添加新记录,或修改当前记录

调用实例 call inputform("select * from mater_bcode","driver={sql server};uid=sa;pwd=passed;database=cthpdb;server=scb-web",1,"null",5)

参数说明

sqlstr: 将要查询的sql语句

connstr: 数据库连接字符串

yesadd: 确定生成的输入表单是用于添加新记录还是修改现有记录

yesadd=1 时是添加新记录,则输入框为空

yesadd=0 时是修改现有记录,则用被修改前的记录内容填充输入框

beginfield:在记录集中开始显示的字段位置

endfield: 在记录集中结束显示的字段位置

idfield: 当inputform被用作修改的输入界面时,往往需要一个id字段来确定是哪条记录将被修改

如果idfield<0 or idfield> rs.fields.count或不是数字,则视为不确定。

coridstr: 控件的id字符串coridstr="null"时 coridstr="fieldvalue"

注意:为了变于在客户端进行输入值的合法性检验,将每个文本框的id以"coridstr + i 的形式确定,例如"fieldvalue0"

corsize: 输入表格中,文本框的长度,之所以用这个参数据是为方便网页的布局,corsize不是数值时,corsize默认为40

on error resume next

dim rs,conn

set conn=server.createobject ("adodb.connection")

conn.open connstr

set rs=server.createobject ("adodb.recordset")

rs.open sqlstr,conn,1,3

if err.number<>0 then

response.write "出现错误:" & err.description

exit function

end if

——–参数处理——————————————

——-处理yesadd,如果yesadd不是数字则默认为添加新记录

if ucase(trim(yesadd))="yes" then yesadd=1 添加新记录

if ucase(trim(yesadd))="no" then yesadd=0 修改记录

if not isnumeric(yesadd) then yesadd=1

if ucase(trim(coridstr))="null" then coridstr="fieldvalue"控件的id字符串,

这样可以解决的在一个页面同调用两次inputform函数时控件id相同的问题

if not isnumeric(corsize) then corsize="40" 控件宽度

———-确定如何显示字段—————–

if not isnumeric(beginfield) or beginfield<0 then

beginfield=0确保开始显示的位置在合理范围内

else

if beginfield=>rs.fields.count -1 then beginfield=rs.fields.count -1保证到少显示一个字段

end if

if ucase(trim(endfield))="max" then endfield=rs.fields.count-1

if not isnumeric(endfield) or endfield<=beginfield then not isnumeric(endfield)用于防止输入的是无效字符串

endfield=rs.fields.count-1

end if

if rs.recordcount<0 then

response.write "取数据时出错!"

exit function

else

—-写入隐藏的id字段

if yesadd=0 and idfield>=0 and (idfield – rs.fields.count<=0) and rs.recordcount>0 then response.write "<input type=hidden name=fieldvalue id=idfield value=" & rs.fields(idfield).value & ">"写入隐藏的id字段

response.write "<table width=75% border=0 bgcolor=#999999 cellspacing=1>"

—-生成表格

dim i

for i=beginfield to endfield

response.write "<tr>"

response.write "<td bgcolor=#cccccc><b><font color=#666666 size=-1>" & rs.fields(i).name & "</font></b></td>"

if yesadd=1 or rs.recordcount=0 then如果不是修改现有记录则输入框为空

if rs.fields(i).type=201 or rs.fields(i).type=202 or rs.fields(i).type=201 then 适用于sql_server

if rs.fields(i).type=201 or rs.fields(i).type=203 then适用于access

response.write "<td bgcolor=#ffffff><textarea type=text name=fieldvalue id=" & coridstr & i & " style= border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 1px rows=5 cols=" & corsize & "></textarea ></td>"

else

response.write "<td bgcolor=#ffffff><input type=text name=fieldvalue id=" & coridstr & i & " style= border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px size=" & corsize & "></td>"

end if

else如果是修改则将原来的值写入输入框

if rs.fields(i).type=201 or rs.fields(i).type=202 or rs.fields(i).type=201 then 适用于sql_server

if rs.fields(i).type=201 or rs.fields(i).type=203 then适用于access

response.write "<td bgcolor=#ffffff><textarea type=text name=fieldvalue id=" & coridstr & i & " style= border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 1px rows=5 cols=" & corsize & ">" & rs(i).value & "</textarea ></td>"

else

response.write "<td bgcolor=#ffffff><input type=text name=fieldvalue id=" & coridstr & i & " style= border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px size=" & corsize & " value=" & rs(i).value & "></td>"

end if

end if

response.write "</tr>"

next

response.write "</table>"

end if

rs.close

conn.close

set rs=nothing

set conn=nothing

err.clear

end function

===================================================

public function executesql(sqlstr,connstr)

executesql功能:用于执行一条sql语句 如deldte、update

参数说明

sqlstr: 将要查询的sql语句

connstr: 数据库连接字符串

返回值

如果返回 "0"是执行成功,否则返回错误号而executesqlput不同,executesql返回err

on error resume next

dim rs,conn

set conn=server.createobject ("adodb.connection")

conn.open connstr

if err.number <> 0 then

response.write writeinfo( "<br>executesql出现错误:<br>" & err.description & "<br>")

executesql=err.number

exit function

else

conn.execute sqlstr

if err.number <>0 then

response.write writeinfo( "<br>executesql出现错误:<br>" & err.description & "<br>")

executesql=err.number

end if

end if

conn.close

set conn=nothing

executesql=err.number

end function

public function executesqlput(sqlstr,connstr)

executesql功能:用于执行一条sql语句,

同executesql不同的是可以有一个返回值。

这个函数主要用于select sum(字段名)、select avg(字段名)之类的sql语句。

参数说明

sqlstr: 将要查询的sql语句

connstr: 数据库连接字符串

返回值

如是返回字符串"err"则表示出错、 这一点与executesql不同

on error resume next

dim rs,conn

set conn=server.createobject ("adodb.connection")

set rs=server.createobject ("adodb.recordset")

conn.open connstr

if err.number <>0 then

response.write writeinfo("<br>executesqlput函数出现错误:<br>" & err.description)

executesqlput="err"

else

rs=conn.execute(sqlstr)

if err.number <>0 then

response.write writeinfo("<br>executesqlput函数运行,出现错误:" & err.description )

executesqlput="err"

else

executesqlput=rs(0)

end if

end if

rs.close

set rs=nothing

conn.close

set conn=nothing

end function

==============================================

用于替换数据库不能保存的字符

public function myreplace(inputstr) 对单个字符进行轮换

inputstr=replace(inputstr,"<","&lt")

inputstr=replace(inputstr,">","&gt")

inputstr=replace(inputstr,"","")

inputstr=replace(inputstr,vbcrlf,"<br>")

myreplace=replace(inputstr,chr(20),"&nbsp;")

end function

public function remyreplace(inputstr) 对单个字符进行还原

inputstr=replace(inputstr,"&lt","<")

inputstr=replace(inputstr,"&gt",">")

inputstr=replace(inputstr,"","")

inputstr=replace(inputstr,"<br>",vbcrlf)

remyreplace=replace(inputstr,"&nbsp;",chr(20))

end function

public function myreplaces(inputstr,active)对字符数组进行转换或还原

参数说明

inputstr 是将要被转换或还原的数组

active 用于确定是进行转换还是还原

active=1时转换成可以在表格中直接显示的超文本格式

active=0时还原成文本文件格式

dim i

if active=1 then

for i=0 to ubound(inputstr)

inputstr(i)=myreplace(inputstr(i))

next

else

for i=0 to ubound(inputstr)

inputstr(i)=remyreplace(inputstr(i))

next

end if

myreplaces=inputstr

end function

public function mytrims(inputstrs)对字符数组中的每个元素去左右空格

dim i

for i=0 to ubound(inputstrs)

inputstrs(i)=trim(inputstrs(i))

next

mytrims=inputstrs

end function

=============================================

public function mytestvalue(myvalues)

将数据valuse的所有元素的值都写在网上,以便查看从其它网页传过来的数组的值

参数据说明:

vaules:待检测的数组

返回值:

如是没有出错返回数组的长度

有错返回“-1”

on error resume next

dim i,valueslen

valueslen=ubound(myvalues)

for i=0 to valueslen

response.write i & " " & myvalues(i) & "<br>"

next

if err.number=0 then

mytestvalue= ubound(myvalues)

else

response.write "<br>mytestvalue 函数运行时发生错误:" & err.description

mytestvalue=-1

end if

end function

public function showupdatesql(sqlstr,connstr,beginfield,endfield,fieldarrayname,arraybeginnum,tablename,sqltype)

showupdatesql函数功能:根据传入的sql语句,快速生成部分"update"语句.

当一条update语句中含有10个或更多的字段时,这个函数将会起到很多作用,不但可以快速后成语句,

而且可以减少错误的发生.

参数说明

sqlstr: 将要查询的sql语句

connstr: 数据库连接字符串

beginfield:在记录集中开始显示的字段位置

endfield: 在记录集中结束显示的字段位置

fieldarrayname:生成update语句时,存入值的数组名

tablename:被查询的表名,之所以用这个参数是为使函数生成的update语更完整些.

sqltype: 确定showupdatesql函数返回是update语句还是insert语句

sqltype="update"时生成update语句,

sqltype=其它值时生成insert语句

调用实例

response.write showupdatesql("select * from abc","driver={sql server};uid=sa;pwd=passed;database=cthpdb;server=scb-web",1,"max","fieldvalue","abc")

返回的结果: update abc set a=fieldvalue(0),b=fieldvalue(1),c=fieldvalue(2)

on error resume next

dim conn,rs

set conn=server.createobject ("adodb.connection")

set rs=server.createobject ("adodb.recordset")

conn.open connstr

rs.open sqlstr,conn,1,3

if err.number <>0 then

response.write writeinfo("<br>showupdatesql函数运行时出现错误!<br>错误信息:" & err.description)

showupdatesql=err.number

exit function

end if

———-确定如何显示字段—————–

if not isnumeric(beginfield) or beginfield<0 then

beginfield=0确保开始显示的位置在合理范围内

else

if beginfield>rs.fields.count-2 then beginfield=rs.fields.count-2保证到少显示一个字段

if beginfield<0 then beginfield=0

end if

if not isnumeric(endfield) then endfield=rs.fields.count-1

if endfield>rs.fields.count-1 or endfield<=beginfield or endfield<0 then endfield=rs.fields.count-1

if not isnumeric(arraybeginnum) then arraybeginnum=0 确定数组的下限是数字

dim i,tempsql

if ucase(trim(sqltype))="update" then

tempsql="&quot;update " & tablename & " set "

for i=beginfield to endfield

tempsql=tempsql & rs(i).name & "=&quot; & " & fieldarrayname & "(" & arraybeginnum & ") & &quot;,"

arraybeginnum=arraybeginnum+1

next

去掉最后一个","和空格

tempsql=left(tempsql,len(tempsql)-1) & "&quot;"

else

dim valuesstr

tempsql="&quot;insert " & tablename & " ("

valuesstr=" values ("

for i=beginfield to endfield

tempsql=tempsql & rs(i).name & ","

valuesstr=valuesstr & "&quot; & " & fieldarrayname & "(" & arraybeginnum & ")" & " & &quot;,"

arraybeginnum=arraybeginnum+1

next

tempsql=left(tempsql,len(tempsql)-1) & ")" & valuesstr

tempsql=left(tempsql,len(tempsql)-1) &")&quot;"

end if

if err.number=0 then

showupdatesql="<br>" & tempsql & "<br><font size=-1 color=#ff0000>注意现在生的sql语句是没有&quot;where&quot; 子句的,<br>这样的sql语句及奇危险的,特别是在生成&quot;updata&quot;语句时,应特别注意!!!!!</font><br>"

else

response.write "<br>&quot; showupdatesql&quot; 函数出错!将返回&quot; -1 &quot; <br>"

showupdatesql=-1

end if

rs.close

conn.close

set rs=nothing

set conn=nothing

err.clear

end function

=========================================================================================================================================================

public function writeinfo(infostr)

response.write "<font size=-1 color=#ff0000>&nbsp;" & infostr & "</font>"

writeinfo="<font size=-1 color=#ff0000>&nbsp;" & infostr & "</font>"

end function

public function myselectmenu(sqlstr,connstr,selname,selid,seledvalue,eventstr)

myselectmenu函数功能是根据sqlstr的记录集生成并填充一个下拉列表框

根据传入的sql语句生成记录后,每条记录的第一项是下拉列表的每个元素的

value,每条记录的第一项是下拉列表的每个元素的text

参数说明:

sqlstr:生成记录集的sql语句

connstr:与数据库联接的字符串

selname:下拉列表框的名字

selid:下拉列表框的id

seledvalue:下拉列表框的默认选项的值,如果ucase(trim(selid))="null"则选中默认第一项即"—-请选择—-"

如果ucase(trim(selid))="myfirst"则是记录集rs的第一条记录的第一个字段

eventstr: 下接列表的事件描述字符串如果eventstr="null"则表示不发生任何事件

返回值:

myselectmenu的返回值为下拉列表的默认选项的值,注意myselectmenu并不一定seledvaluebn 也可能是-1

调用实例:

selsql="select user_code,user_name from inter_user"

response.write "<td bgcolor=#ffffff>"

call myselectmenu(selsql,connstr,"fieldvalue","fieldvalue" & i,"null")

response.write "</td>"

上面的代码是将在表格的一个格中放置一个填充好的下拉列表.

on error resume next

dim rs,conn

set conn=server.createobject ("adodb.connection")

conn.open connstr

set rs=server.createobject ("adodb.recordset")

rs.open sqlstr,conn,1,3

if err.number<>0 then

response.write "myselectmenu函数在运行时出现错误:<br>" & err.description

myselectmenu=-1

exit function

end if

与下拉列表控件相关参数处理

if ucase(trim(eventstr))="null" then eventstr=""

if ucase(trim(selid))="null" then selid=selnameid的默认为与name相同

response.write "<select name=" & selname & " id=" & selid & " style= border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 1px " & eventstr &">"

if ucase(trim(seledvalue))="null" then 如果指定下拉列表的没有默认值则,则它的默认值为第一项

response.write "<option value=-1 selected>—-请选择—-</option>"

myselectmenu=-1

else好象没有什么意义,

response.write "<option value=-1>—-请选择—-</option>"

end if

if rs.recordcount>0 then

dim i

for i=0 to rs.recordcount

if i=0 and ucase(trim(seledvalue))="myfirst" then ucase(trim(seledvalue))="myfirst"表示下拉列表的默认选定值是—-请选择—-后的第一个可用项.

response.write "<option value=" & rs(0) & "selected>" & rs(1) & "</option>"

myselectmenu=rs(0)

else

if trim(rs(0))=trim(seledvalue) then注意如果数据库的结果是经过转换可能会没有一样的值

response.write "<option value=" & rs(0) & "selected>" & rs(1) & "</option>"

myselectmenu=rs(0)

else

response.write "<option value=" & rs(0) & " >" & rs(1) & "</option>"

end if

end if

rs.movenext

next

end if

response.write "</select>"

if myselectmenu="" then myselectmenu=-1

rs.close

conn.close

set rs=nothing

set conn=nothing

err.clear

end function

public function aspalert(infostr)

aspalert函数功能是在客户端弹出一个消息框

参数说明:

infostr 是将要弹出的信息文本

调用实例: call aspalert("成功!")

response.write "<script language=javascript>"

response.write "alert(" & infostr & ");"

response.write "</script>"

end function

public function asplocation(httpstr)用于在客户端窗体的转向

如是直接用response.redirect无法使aspalert弹出对话框

参数说明:

httpstr 将要转向到的网址

调用实例:

dim httpstr

httpstr="http://www.clkhome/mater_code1/class/super_class/addnew.asp"

call asplocation(httpstr)

response.write "<script language=javascript>"

response.write "window.location.href=" & httpstr &";"

response.write "</script>"

end function

public function aspback(backstep)用于在客户端窗本后退

如是直接用response.redirect无法使用aspalert弹出对话框

参数说明:

backstep 窗本后退的步数

调用实例:

call aspback(-2)回退两步

—–参数处理——

if not isnumeric(backstep) then backstep=-1

response.write "<script language=javascript>"

response.write "window.history.back(" & backstep & ");"

response.write "</script>"

end function

================================================================================

下面是几个调用的例子

dim sqlstr,constr

constr="dsn=clkdb;uid=sa;pwd=passed"

这里用的是odbc数据源,用到其它机器上时,注意要修改这里才能正常运行

constr1="driver={sql server};uid=sa;pwd=passed;database=cthpdb;server=sab"

sqlstr=trim(request("sqlstr"))

——显示表格

call datagrid("select * from mater_bcode",constr,10,20,2,50,0,"http://www.clkhome/mater_code1/tools/aaa.asp")

response.write datagrid("select * from mater_bcode",constr,10,20,2,7,0,"0")

——输入表格

call inputform("select id as id, mete_sort_name as 大类名称, mete_class_code as 大类代码 from meterial_vigo",constr,0,0,"max",0)

全文完

=============================================

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 我在开发过程总结的一套实现常用功能的函数
分享到: 更多 (0)

相关推荐

  • 暂无文章