首页 > 开发 > Asp > 正文

ASP常用函数收集

2019-10-20 15:24:25
字体:
来源:转载
供稿:网友

Oledb方式打开SQL数据连接
Sub OpenSqlByOledb 
   on error resume next
   set conn=server.createobject("adodb.connection")
   conn.open "Provider=SQLOLEDB.1;Password='sa';Persist Security Info=true;User ID=sa;Initial Catalog=gate;Data Source=(local);"
   If err.number<>0 Then
    err.clear
    Response.Write("<div align='center'>数据库连接出错,请联系管理员!</div>")
    Response.End()
   End If
End Sub

'数据库操作相关函数
'************************************************************
'[Sub]openReadRs rs,sql,oType
'功能:打开对应类型的数据集,只读操作
'参数:
'    rs 打开的rs集合
'    sql 执行的SQL语句
'************************************************************
Sub openReadRs(rs,sql)
 If Not IsObject(rs) Then
  Set rs=Server.CreateObject("ADODB.Recordset")
  rs.open sql,conn,1,1  
 End If
End Sub
'************************************************************
'[Sub]openWriteRs rs,sql
'功能:打开对应类型的数据集,可写入操作
'参数:
'    rs 打开的rs集合
'    sql 执行的SQL语句
'************************************************************
Sub openWriteRs(rs,sql)
 If Not IsObject(rs) Then
  Set rs=Server.CreateObject("ADODB.Recordset")
  rs.open sql,conn,1,3
 End If
End Sub
'************************************************************
'[Sub]endRs rs
'功能:关闭对应类型的数据集
'参数:
'    rs 打开的rs集合
'************************************************************
Sub endRs(rs)
 If IsObject(rs) Then
  rs.close:Set rs=Nothing
 End If
End Sub
'************************************************************
'[Sub]endConn
'功能:关闭数据库连接
'参数:无
'************************************************************
Sub endConn
 If IsObject(conn) Then
  conn.close:Set conn=Nothing
 End If
End Sub
'************************************************************
'[Sub]execSql sql
'功能:执行SQL语句
'参数:
'    sql 执行的SQL语句
'************************************************************
Sub execSql(sql)
 conn.execute(sql)
End Sub
'************************************************************
'[]selectAlone sql
'功能:单一字段查询
'参数:
'    sql 执行的SQL语句
'返回:查询字段值
'************************************************************
selectAlone(sql)
 Dim selectAloneRs
 Set selectAloneRs=conn.execute(Sql)
 selectAlone=selectAloneRs(0)
 endRs selectAloneRs
End

 

'字符串操作相关函数
'************************************************************
'[]IsSafeStr str
'功能:判断是否安全字符串,在注册登录等特殊字段中使用
'参数:
'    str 需检测的字符串
'返回:true=安全,false=不安全
'************************************************************
IsSafeStr(str) 
 Dim s_BadStr, n, i
 s_BadStr = "'   &<>?%,;:()`~!@#$^*{}[]|+-=" & Chr(34) & Chr(9) & Chr(32)
 n = Len(s_BadStr)
 IsSafeStr = True
 For i = 1 To n
  If Instr(str, Mid(s_BadStr, i, 1)) > 0 Then
   IsSafeStr = False
   Exit 
  End If
 Next
End 
'************************************************************
'[]HtmlToEncode str
'功能:将HTML编码
'参数:
'    str 需编码的字符串
'返回:编码后的字符串
'************************************************************
HtmlToEncode(str) '
 if not isnull(str) and str<>"" then
  str = Replace(str,"&","&amp;") 
  str = replace(str, ">", "&gt;")
  str = replace(str, "<", "&lt;")
  str = Replace(str, CHR(32), " ")
  str = Replace(str, CHR(9), "&nbsp;")
  str = Replace(str, CHR(34), "&quot;")
  str = Replace(str, CHR(39), "&#39;")
  str = Replace(str, CHR(13), "")
  str = Replace(str, "", "&#115;cript")
  str = Replace(str, "&#115;", "&#115;")
  HtmlToEncode = str
 end if
End 
'************************************************************
'[]EncodeToHtml str
'功能:将HTML反编码
'参数:
'    str 需反编码的字符串
'返回:反编码后的字符串
'************************************************************
EncodeToHtml(str)
 if not isnull(str) and str<>"" then
  str = Replace(str,"&amp;","&") 
  str = replace(str, "&gt;", ">")
  str = replace(str, "&lt;", "<")
  str = Replace(str, " ", CHR(32))
  str = Replace(str, "&nbsp;", CHR(9))
  str = Replace(str, "&quot;", CHR(34))
  str = Replace(str, "&#39;", CHR(39))
  str = Replace(str, "", CHR(13))
  str = Replace(str, "&#115;cript", "")
  str = Replace(str, "&#115;", "&#115;")
  EncodeToHtml = str
 end if
End 
'************************************************************
'[]gotTopic str,strlen
'功能:截取字符串strlen长度,汉字算2长度
'参数:
'    str 需截取的字符串
'    strlen 截取长度
'返回:截取strlen长度后的字符串
'************************************************************
getTopic(str,strlen)
 if str="" then
  gotTopic=""
  exit 
 end if
 dim l,t,c, i
 l=len(str)
 t=0
 for i=1 to l
  c=Abs(Asc(Mid(str,i,1)))
  if c>255 then
   t=t+2
  else
   t=t+1
  end if
  if t>=strlen then
   gotTopic=left(str,i) & "…"
   exit for
  else
   gotTopic=str
  end if
 next
End 
'************************************************************
'[]DateFormatTime Tvar,sType
'功能:时间格式化
'参数:
'    Tvar 需要格式化的时间字符串
'    sType 格式化类型
'返回:格式化后的时间字符串
'************************************************************
DateFormatTime(Tvar,sType)
 dim Tt,sYear,sMonth,sDay,sHour,sMinute,sSecond
 If Not IsDate(Tvar) Then DateFormatTime = "" : Exit 
 Tt   = Tvar
 sYear  = Year(Tt)
 sMonth = Right("0" & Month(Tt),2)
 sDay  = Right("0" & Day(Tt),2)
 sHour  = Right("0" & Hour(Tt),2)
 sMinute = Right("0" & Minute(Tt),2)
 sSecond = Right("0" & Second(Tt),2)
 Select Case sType
  Case 1 '2006-3-13
   DateFormatTime = sYear & "-" & sMonth & "-" & sDay
  Case 2 '2006年3月13日
   DateFormatTime = sYear & "年" & sMonth & "月" & sDay & "日"
  case 3  '200603131120123
   DateFormatTime = sYear & sMonth & sDay & sHour & sMinute &sSecond
  Case Else
   DateFormatTime = Tt
  End Select
End 
'************************************************************
'[]IsNum str
'功能:是否为数字
'参数:
'    str 需要判断的字符串
'返回:true=数字,false=非数字
'************************************************************
IsNum(Str) '
 if Str<>"" and isnumeric(Str) then
  IsNum=True
 else
  IsNum=False
 end if
End

 

'输出调试相关函数
'************************************************************
'[Sub]mesGoBack str
'功能:弹出信息并返回
'参数:
'    str 需要判断的字符串
'************************************************************
Sub mesGoBack(str)
 Response.write("< language='java'>alert('"&str&"');history.back();</>")
 Response.End()
End Sub
'************************************************************
'[Sub]mesGoUrl str,url
'功能:弹出信息并返回
'参数:
'    str 需要判断的字符串
'    url 跳转地址
'************************************************************
Sub mesGoUrl(str,url)
 Response.write("< language='java'>alert('"&str&"');location.href='"&url&"';</>")
 Response.End()
End Sub
'************************************************************
'[Sub]Debug Wrong
'功能:调试输出
'参数:
'    Wrong 希望输出信息
'返回:无返回值
'************************************************************
Sub Debug(Wrong) '调试输出
 Response.Write(Wrong)
 Response.End()
End Sub

 

'功能相关函数
'************************************************************
'[]getIP
'功能:获取用户IP
'参数:
'   无
'返回:用户当前IP
'************************************************************
getIP()
 Dim userip
 userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR") 
 If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR") 
 getIP=userip
End 
'************************************************************
'[]getUrl
'功能:获取当前域名
'参数:
'    无
'返回:当前域名
'************************************************************
getUrl()
 getUrl=Request.ServerVariables("Server_name")
End

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表