-

asp(VBScript)介绍(图文教程)

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境,可用来创建动态交互式网页并建立强大的web应用程序。

在 VBScript 中,Int 函数和 CInt 函数和 CDbl 函数区别

在 VBScript 中,Int 函数和 CInt 函数和 CDbl 函数都可以将值转换为整数。然而,它们也存在一些重要的区别。

Int 函数将值向零舍入到最接近的整数,这意味着它会将数字舍弃小数部分。例如,Int(4.5) 的结果为 4,Int(-4.5) 的结果为 -5。

CInt 函数也将值转换为整数,但是它会将小数部分四舍五入到最接近的整数。例如,CInt(4.5) 的结果为 5,CInt(-4.5) 的结果为 -4。

尝试一下>>
<%
call echo("提示","在 VBScript 中,Int 函数和 CInt、CDbl 函数区别")
dim i 
for i=0 to 2
    call echo(i, "("& i &"/10)=" & (i/10) )
    call echo("int("& i &"/10)", int(i/10))
    call echo("cint("& i &"/10)", cint(i/10))
    call echo("CDbl("& i &"/10)", CDbl(i/10))
    call hr()
next
call echo("int(""0.6"")",int("0.6"))
call echo("cint(""0.6"")",cint("0.6"))
call echo("CDbl(""0.6"")",CDbl("0.6"))
call hr()

call echo("int(0.6)",int(0.6))
call echo("cint(0.6)",cint(0.6))
call echo("CDbl(0.6)",CDbl(0.6))
call hr()

call echo("int(""1.999"")",int("1.999"))
call echo("cint(""1.999"")",cint("1.999"))
call echo("CDbl(""1.999"")",CDbl("1.999"))
call hr()

call echo("int(""-4.5"")",int("-4.5"))
call echo("cint(""-4.5"")",cint("-4.5"))
call echo("CDbl(""-4.5"")",CDbl("-4.5"))
call hr() 
%>
DateAdd函数中日期相加或相减使用方法
DateAdd是用于在指定日期上添加或减去指定的时间间隔
interval 参数可以有以下值:
yyyy 年
q 季度
m 月
y 一年的日数
d 日
w 一周的日数
ww 周
h 小时
n 分钟
s 秒
尝试一下>>
<%
' yyyy  年
' q 季度
' m 月
' y 一年的日数
' d 日
' w 一周的日数
' ww    周
' h 小时
' n 分钟
' s 秒 

response.write(" 当前时间: " & now() & "<hr>")
call debug("当前时间+1年","yyyy")
call debug("当前时间+1季度","q")
call debug("当前时间+1月","m")
call debug("当前时间+1一年的日数","y")
call debug("当前时间+1日","d")
call debug("当前时间+1一周的日数","w")
call debug("当前时间+1周","ww")
call debug("当前时间+1小时","h")
call debug("当前时间+1分钟","n")
call debug("当前时间+1秒","s")

function debug(tip,interval)
    response.write( Now() & "<br>")
    response.write(dateAdd(interval,1,now())  & "     " & tip)
    response.write("<hr>")  
end function

dim s
s=" 2024/1/2"
response.write("s=" & s & "<br>")
response.write("(加一月)s=" & dateAdd("m",1,s))    ' 如果s为2024/0/2  则为报错'


%>
DateDiff 函数是计算两个日期之间的差异
DateDiff 函数是计算两个日期之间的差异
interval 参数可以有以下值:
yyyy 年
q 季度
m 月
y 一年的日数
d 日
w 一周的日数
ww 周
h 小时
n 分钟
s 秒
尝试一下>>
<%
dim setTime
setTime="2023/3/2 12:03:25"
response.write(" 当前时间: " & setTime & "
") call debug("获得年","yyyy") call debug("获得季度","q") call debug("获得月","m") call debug("获得一年的日数","y") call debug("获得日","d") call debug("获得一周的日数","w") call debug("获得周","ww") call debug("获得小时","h") call debug("获得分钟","n") call debug("获得秒","s") function debug(tip,interval) response.write( Now() & "
") response.write(dateDiff(interval,setTime,now()) & "     " & tip + " ("& interval &")") response.write("
") end function %>
WinHttp发送POST
WinHttp发送POST,可设置post值,和cookies值
cookie里ASPSESSIONIDQQQQSSDR 为服务器验证块,把登录浏览器里的cookies块传给cookiesStr参数里,可获得需要登录验证的网页的

<%
call case1() '案例1'
sub case1()
  dim httpurl, refererUrl, cookiesStr, postStr, sSetChar

  httpurl="http://127.0.0.1/5.asp?act=test&page=1&id=6"
  cookiesStr="user=123456; pass=test; ASPSESSIONIDQQQQSSDR=PDKLNMNBJNIPKCDDDIMCKFCG; sex=1"  
  postStr="username=sh001&password=123456&captcha=2802&rememberMe=true&test=中文字符" 

 call echo("getWinHttp",getWinHttp(httpurl,refererUrl,cookiesStr,postStr,"utf-8"))
 call hr()
 call echo("getWinHttpCookies",getWinHttpCookies(httpurl,refererUrl,cookiesStr,postStr,"utf-8"))


end sub
%>
更新数据表报错
在Access里更新会报错 因为上面的lastlogintime时间更新了,而openconn又重新连接了数据库了

<!--#Include virtual = "/Inc/Config.Asp"-->


<a href="?act=save&id=0">更新id=0(报错)</a> | 
<a href="?act=save&id=2">更新id=2(正常)</a> | 
<a href="?%>">刷新</a> | <br>
<%
doevents

'openconn函数里要加个判断 代码如下:
' if isNul(conn)=false then
'   exit sub
' end if
 
call openconn()   
rsx.open"select * from ["& db_PREFIX &"Admin] where id=0",conn,1,3
'更新下状态时间 20230304'
rsx("lastlogintime")=now()'最后登录时间 如果这里注释掉,下面的更新也不会错误
rsx.update

call openconn() 
dim id
id=request("id")

if request("act")="save" then
    call echo("id",id):doevents
    rs.open"select * from ["& db_PREFIX &"Admin] where id="&id,conn,1,3
    if not rs.eof then

    rs("username")="aaabbb333_" & getrnd(8)
    rs.update    '在Access里更新会报错 因为上面的lastlogintime时间更新了,而openconn又重新连接了数据库了'
    call echo("username",rs("username"))
    else
        call echoRed("提示","id("&  id &")不存在")
    end if
end if
%>
代码块错误判断
错误忽略:on error resume next
错误清除:err.clear
恢复正常的错误处理:on error goto 0

<%option explicit
response.addheader "Content-Type", "text/html; charset=utf-8" 
on error resume next
    response.write(aa)

    ' 检查是否发生错误
    if err.number<>0 then
        response.write("错误: " & err.description)
        err.clear 
    end if

' 恢复正常的错误处理
on error goto 0

response.write(bb)
%>
清除缓冲显示后面内容
清除缓冲,不显示上面的内容,只显示下面的内容,如何做
<%
response.write("aaa")
'清除缓冲,不显示上面的内容,只显示下面的内容,如何做'
response.buffer = true
response.clear()
response.write("bbb")
response.flush()
%>