欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一个改进的ASP生成SQL命令字符串类的代码[已测]

程序员文章站 2022-06-21 22:39:28
复制代码 代码如下:<% class sqlstring '************************************ '变量定义 '*********...

复制代码 代码如下:

<%
class sqlstring
'************************************
'变量定义
'************************************
'stablename ---- 表名
'isqltype ----sql语句类型:0-增加,1-更新,2-删除,3-查询
'swhere ---- 条件
'sorder ---- 排序方式
'ssql ----值
private stablename,isqltype,swhere,sorder,ssql
'************************************
'类初始化/结束
'************************************
private sub class_initialize()
stablename=""
isqltype=0
swhere=""
sorder=""
ssql=""
end sub
private sub class_terminate()
end sub
'************************************
'属性
'************************************
'设置表名的属性
public property let tablename(value)
stablename=value
end property
'设置条件
public property let where(value)
swhere=value
end property
'设置排序方式
public property let order(value)
sorder=value
end property
'设置查询语句的类型
public property let sqltype(value)
isqltype=value
select case isqltype
case 0
ssql="insert into {&*#}0 ({&*#}1) values ({&*#}2)"
case 1
ssql="update {&*#}0 set {&*#}1={&*#}2"
case 2
ssql="delete from {&*#}0 "
case 3
ssql="select {&*#}1 from {&*#}0 "
end select
end property
'************************************
'函数
'************************************
'增加字段(字段名称,字段值)
public sub addfield(sfieldname,svalue)
select case isqltype
case 0
ssql=replace(ssql,"{&*#}1",sfieldname & ",{&*#}1")
ssql=replace(ssql,"{&*#}2","" & svalue & ",{&*#}2")
case 1
ssql=replace(ssql,"{&*#}1",sfieldname)
ssql=replace(ssql,"{&*#}2","" & svalue & ",{&*#}1={&*#}2")
case 3
ssql=replace(ssql,"{&*#}1",sfieldname & ",{&*#}1")
end select
end sub
'修改的返回字符串值的函数
'返回sql语句
public function returnsql()
ssql=replace(ssql,"{&*#}0",stablename)
select case isqltype
case 0
ssql=replace(ssql,",{&*#}1","")
ssql=replace(ssql,",{&*#}2","")
case 1
ssql=replace(ssql,",{&*#}1={&*#}2","")
case 3
ssql=replace(ssql,",{&*#}1","")
end select
if swhere<>"" and isqltype<>0 then
ssql=ssql & " where " & swhere
end if
if sorder<>"" and isqltype<>0 then
ssql=ssql & " order by " & sorder
end if
returnsql=ssql
end function
'返回sql语句
public function returnsql1()
ssql=replace(ssql,"{&*#}0",stablename)
select case isqltype
case 0
ssql=replace(ssql,",{&*#}1","")
ssql=replace(ssql,",{&*#}2","")
case 1
ssql=replace(ssql,",{&*#}1={&*#}2","")
case 3
ssql=replace(ssql,",{&*#}1","")
end select
if swhere<>"" and isqltype<>0 then
ssql=ssql & " where " & swhere
end if
if sorder<>"" and isqltype<>0 then
ssql=ssql & " order by " & sorder
end if
returnsql=ssql
end function
'清空语句
public sub clear()
stablename=""
isqltype=0
swhere=""
sorder=""
ssql=""
end sub
end class
%>
调用例子:
<%
set a =new sqlstring '创建类对象
a.tablename=" message " '设置表名为message
'a.where=" issend =9"
'a.order=" issend desc"
a.sqltype=0 '设置查询类型为增加记录
a.addfield " incept", "'2'"
a.addfield " sender ", "'%3%' "
a.addfield " title ", "#"&now&"#"
a.addfield " sender ", "5 "
a.addfield " content ", " 6 "
a.addfield " sendtime ", "7"
a.addfield " flag", 8
a.addfield " issend ", 9
response.write a.returnsql
set a=nothing
%>
<%
set a =new sqlstring '创建类对象
a.tablename=" message " '设置表名为message
'a.where=" issend =9"
'a.order=" issend desc"
a.sqltype=0 '设置查询类型为增加记录
a.addfield " incept", "'2'"
a.addfield " sender ", "'%3%' "
a.addfield " title ", "#"&now&"#"
a.addfield " sender ", "5 "
a.addfield " content ", " 6 "
a.addfield " sendtime ", "7"
a.addfield " flag", 8
a.addfield " issend ", 9
response.write a.returnsql
set a=nothing
%>
<%
set a =new sqlstring '创建类对象
a.tablename=" message " '设置表名为message
'a.where=" issend =9"
'a.order=" issend desc"
a.sqltype=0 '设置查询类型为增加记录
a.addfield " incept", "'2'"
a.addfield " sender ", "'%3%' "
a.addfield " title ", "#"&now&"#"
a.addfield " sender ", "5 "
a.addfield " content ", " 6 "
a.addfield " sendtime ", "7"
a.addfield " flag", 8
a.addfield " issend ", 9
response.write a.returnsql
set a=nothing
%>