自己写的一种很爽的用参数访问数据库的方法,避免了字符串拼接sql,拿出来晒晒......

bwangel 2008-04-03 11:17:06
库函数代码可能很长,但调用起来非常简单。
举例

执行更新:
sql = "UPDATE T_Reply SET ReplyText=?, SideCode=?, UserID=? WHERE ID=?"
Ps = Array(ReplyText,SideCode,UserID,CaseID)
Call ExecNonQuery(sql, Ps)


执行查询:
dim id,userid
id = ...
userid = ...

set rs = ExecRs("SELECT * FROM T_Advice WHERE ID=? AND UserID=?", Array(id, UserID))

do while not rs.eof
...
rs.movenext
loop

....
rs.close
set rs=nothing

个人认为最大的好处是避免了字符串拼接,从而有效防止了sql注入。再也不用过滤来过滤去了。
其次是调用简洁,再也不用写一大串什么的 set什么 conn.open什么的了。

ExecRs()函数返回的记录集是那种只进的记录集。虽然功能没有那种可以update的记录集多,但速度快,资源少。至于分页,用另外专门的函数(在我另外一个贴子里),速度也比传统分页快得多。

以下是被调用的库(sql 2000, 使用nchar和nvarchar):

Dim Conn ,rs, sql,i
Sub OpenConn()
if IsObject(Conn) then exit sub
Dim ConnStr
ConnStr = "Provider = Sqloledb; User ID =XXXX; Password =XXXX; Initial Catalog =XXXX; Data Source=." '填上自己的数据库信息
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open ConnStr
End Sub

Sub CloseConn()
If IsObject(Conn) Then
Conn.Close
Set Conn = Nothing
End If
End Sub

'the Content below added by wang 2008-3-13
'准备参数
private function parpareCmd(sql,ps)
OpenConn
dim p, cmd, rs , pnum
set cmd = Server.CreateObject("Adodb.Command")
cmd.CommandText = sql
cmd.ActiveConnection = Conn
pnum = ParamNum(sql)

'检查参数个数是否小于必需数目。
'CreateParameter([Name] , [type] , [Direction] , [Size] , [Value] )
if (not isnull(ps)) then
if (ubound(ps)+1) < pnum then
Response.write "参数个数应该是 " & pnum & ". 但实际只传递了" & (ubound(ps)+1)
Response.end
end if
for i = 0 to pnum-1
set p = cmd.CreateParameter( , 130, ,chkLength(Ps(i)), Ps(i))
'response.write "P" & I & "=" & Ps(i) & "L:" &chkLength(Ps(i)) & "<br />"
cmd.Parameters.Append p
next
end if
set parpareCmd = cmd
end function

'通过sql中'?'号的个数得知参数个数
function ParamNum(sql)
ParamNum = 0
for i=1 to LEN(sql)
if mid(sql,i,1)="?" then ParamNum = ParamNum + 1
next
end function

'执行更新查询
function ExecNonQuery(sql,ps)
dim cmd,rs
set cmd = parpareCmd(sql,ps)
set rs = cmd.Execute
set cmd=nothing
set rs=nothing
end function

'返回单个整数,如果为空则当作0处理
function ExecInt(sql, ps)
dim cmd,rs
set cmd = parpareCmd(sql,ps)
set rs = cmd.Execute
ExecInt=TurnInt(Rs(0))
set rs=nothing
set cmd=nothing
end function

'返回单个字符串,如果为空则当作空串处理
function ExecStr(sql, ps)
dim cmd,rs
set cmd = parpareCmd(sql,ps)
set rs = cmd.Execute
if isnull(Rs(0)) then
ExecStr = ""
else
ExecStr=CStr(Rs(0))
end if
set rs=nothing
set cmd=nothing
end function

'返回一个记录集
function ExecRs(sql, ps)
dim cmd,rs
set cmd = parpareCmd(sql,ps)
set rs = cmd.Execute()
set ExecRs=rs
set cmd=nothing
end function

function chkLength(s)
if isnull(s) then
chkLength = 2
else
chkLength = max(2,len(s))
end if
end function

'数字转换,将null值视为0处理
function TurnInt(c)
if isnull(c) or c="" or not IsNumeric(c) then
TurnInt=0
else
TurnInt=CLng(c)
end if
end function

...全文
648 54 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
54 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangpeng2003 2008-04-09
  • 打赏
  • 举报
回复
mark
分就不要了^_^
xiaojing7 2008-04-09
  • 打赏
  • 举报
回复
mark
mataofq 2008-04-09
  • 打赏
  • 举报
回复
code is everything !
ice241018 2008-04-09
  • 打赏
  • 举报
回复
up
net86 2008-04-09
  • 打赏
  • 举报
回复
学习加接分
pgdoryoku 2008-04-09
  • 打赏
  • 举报
回复
学习~
BoyHaXin 2008-04-09
  • 打赏
  • 举报
回复
you fen yiu jie
liaoliming 2008-04-08
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 cesar200 的回复:]
慢慢看!!!!
[/Quote]
wolfgang_l 2008-04-08
  • 打赏
  • 举报
回复
顶楼主
ymle1228 2008-04-08
  • 打赏
  • 举报
回复
接分
bj20082005 2008-04-08
  • 打赏
  • 举报
回复
mark
lsf5921 2008-04-08
  • 打赏
  • 举报
回复
我来学习
arorn 2008-04-08
  • 打赏
  • 举报
回复
学习一下!顺便接着分
sniper1534 2008-04-08
  • 打赏
  • 举报
回复
jf
JlinShu 2008-04-08
  • 打赏
  • 举报
回复
学习
liyong888_8 2008-04-08
  • 打赏
  • 举报
回复
学习
jf
bcc1o 2008-04-08
  • 打赏
  • 举报
回复
复制下来先
lb3376 2008-04-05
  • 打赏
  • 举报
回复
不错!
顶一下,接分。
winner2050 2008-04-05
  • 打赏
  • 举报
回复
还是asp.net NB
xwwlgs 2008-04-05
  • 打赏
  • 举报
回复
路过~~
加载更多回复(34)

28,409

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧