俩函数搞定asp的orm映射[原创]
俩函数搞定asp的orm映射
orm必须用到实体类,像C#这样的语言 写实体类挺痛苦的,除非用工具
而asp有个好处,因为他可以动态构建一个字符串并把此字符串动态解析为代码,也就是他的 execute 和 ExecuteGlobal 俩函数
下面这个函数是 实体类生成器 只要传入列名字符串就能生成 一个全局可用的类
'定义一个实体类
'clsname 自定义类名
'cols 列字符串逗号分隔
sub clsnew(clsname,cols)
dim p
p = "class "&clsname&" :public "&cols&":public cols:sub class_initialize():cols="""&cols&""":end sub:sub fill(a,rownum):dim b,i:b = split(cols,"",""):for i=0 to ubound(b):execute b(i)&""=""&a(i,rownum):next:end sub: end class"
'一个动态的类 通过传入逗号分隔的列名字符串 来定义类内部的变量属性
'实体类内部带有一个填充自己的方法,用结果集的二维数组来填充
ExecuteGlobal p
'全局执行完 以后 本页面到处可用此类了
end sub
'二维数组表转化为实体类数组
'clsname 类名
'a 记录集的二维数组
function orm(clsname,a)
dim r(),i,o
redim r(ubound(a,2))
for i=0 to ubound(a,2)
execute "set o = new "&clsname
o.fill a,i
set r(i)=o
next
orm = r
end function
'使用实例
sub testorm
dim cols,clsname
cols = "id,bookid,type1"
clsname = "booktype"
dim a,b,i
conn_open
getrsa "select top 20 id,bookid,type type1 from book_type",a
'getrsa是执行sql并取得结果数组的函数
'a是通过rs.getrows获得的二维数组
conn_close
clsnew clsname,cols
b = orm(clsname,a)
for i=0 to ubound(b)
echo b(i).id&" "&b(i).bookid&" "&b(i).type1&"<br/>"
'这里我们就达到了目的 再也不用写rs("id")这样的了 用点代替了括号和双引号
next
end sub
'附 执行sql的函数
sub getrsa(sql,a)
dim rs
set rs = conn.execute(sql)
if not rs.eof then
a = rs.getrows()
else
a = ""
end if
set rs = nothing
end sub
相关文档:
Active Server Pages 提供内建对象,这些对象使用户更容易收集通过浏览器请求发送的信息、响应浏览器以及存储用户信息(如用户首选项)。本文简要说明每一个对象。
Application 对象
可以使用 Application 对象使给定应用程序的所有用户共享信息。
Request 对象
可以使用 Request 对象访问任何用 HTTP 请求 ......
asp 中常用的文件处理函数 收藏
asp 中处理文件上传以及删除时常用的自定义函数
<%
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'所有自定义的VBS函数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function DeleteFile(Filename) '删除文件
&nbs ......
'creat by qqlxinye@tom.com
'time 2008-09-19
'qq:273453129
'web www.qqlxinye.cn
dim outSmtp,outUser,outPsd,recUser,recSubmit,bodyContent,AddAttachment,ifsend
function init_mail(str1,str2,str3,str4,str5,str6,str7)
outSmtp=str1
outUser=str2
outPsd=str3
recUser=str ......
<%
'下面是最具技术含量的函数了,哈哈~
'增加数据库字段
function addziduan(ziduanming,ziduanleixing)
on error resume next
fldname = ziduanming
if ziduanleixing=1 or ziduanleixing=4 then
fldtype = "VarChar"
fldsize = 255
elseif ziduanleixing=2 or ziduanleixing=3 then
fldtyp ......
fix是bmp的一种衍生格式,可用于asp生成验证码图形。
一个例子:
1 图象绘制完成后,将文件存为24位位图格式的BMP图象文件。
2 去除前54Byte内容.
3 改名为BODY+数字.FIX
4 修改CHECKCODE.ASP,调用你新做的这个文件.
(checkcode.asp 负责生成和输出验证码 1309 byte,head.fix 是54byte的BMP头,body.fix 数字0~9 10 ......