ASP常用函数

来源:互联网 发布:如何查询数据库地址吗 编辑:程序博客网 时间:2024/06/10 03:28

'************************************************************************************
'作用:分页代码
'totalnumber总记录;maxperpage,页大小;filename,文件名;Contial,条件(&**=??&**=??)
'************************************************************************************
Function showpage(totalnumber,maxperpage,filename,Contial) 
Dim n

If totalnumber Mod maxperpage=0 Then 
 n= totalnumber / maxperpage 
Else
 n= totalnumber / maxperpage+1 
End If

Response.Write "<form method=Post action="&filename&"?"& Contial &" >" 
Response.Write "<p align='center' class='contents'> " 
If Page<2 Then 
 Response.Write "<font class='contents'>首页 上一页</font> " 
Else 
 Response.Write "<a href="&filename&"?page=1"& Contial &" class='contents'>首页</a> " 
 Response.Write "<a href="&filename&"?page="&Page-1&""& Contial &" class='contents'>上一页</a> " 
End If

If n-Page<1 Then 
 Response.Write "<font class='contents'>下一页 尾页</font>" 
Else 
 Response.Write "<a href="&filename&"?page="&(Page+1)&""& Contial &" class='contents'>" 
 Response.Write "下一页</a> <a href="&filename&"?page="&n&""& Contial &" class='contents'>尾页</a>" 
End If 
 Response.Write "<font class='contents'> 页次:</font><font class='contents'>"&Page&"</font><font class='contents'>/"&n&"页</font> " 
 Response.Write "<font class='contents'> 共有"&totalnumber&"条新闻 "
 Response.Write "<font class='contents'>转到:</font><input type='text' name='page' size=2 maxlength=10 class=smallInput value="&Page&">" 
 Response.Write "&nbsp;<input type='submit'  class='contents' value='GO' name='cndok' ></form>" 
End Function  

'************************************************************************************
'作用:计算大小,返回带有单位的值
'Unit,输入值的单位,F_Size,输入值
'************************************************************************************
Function SizeOf(F_Size,Unit)
 If F_Size = "" Then
  SizeOf = "0K"
  Exit Function
 End If
 F_Size = Replace(Replace(Replace(Ucase(F_Size),"K",""),"M",""),"B","")
 If F_Size = "" Then
  SizeOf = "0K"
  Exit Function
 ELse
  Dim C,i,tem
  For i = 1 To Len(F_Size)
   C = Mid(F_Size,i,1)
   If Not IsNumeric(C) Then
    SizeOf = "0K"
    Exit Function
   End If
  Next
  F_Size = Cdbl(F_Size)
  Select Case Unit
   Case "K"
    If F_Size >= 1024 Then F_Size = F_Size / 1024 : Unit = "MB"
    If F_Size >= 1024 Then F_Size = F_Size / 1024 : Unit = "MB"
    tem = Round(F_Size,2) & Unit
   Case Else
    If F_Size >= 1024 Then F_Size = F_Size / 1024 : Unit = "MB"
    If F_Size >= 1024 Then F_Size = F_Size / 1024 : Unit = "MB"
    tem = Round(F_Size,2) & "K"
  End Select
  SizeOf = tem
 End If
End Function

 

'*************************************
'检验邮箱
'*************************************
Function CheckEmail(strEmail)
    Dim re
    Set re = New RegExp
    re.Pattern = "^[/w-/.]{1,}/@([/da-zA-Z-]{1,}/.){1,}[/da-zA-Z-]{2,3}$"
    re.IgnoreCase = True
    CheckEmail = re.Test(strEmail)
End Function

 

'---------------------------------------------------------------------------------------------------------------------------------------
'功能:防止SQl注入 
'参数:Str,字符串
'说明:
'时间:
'作者:
'---------------------------------------------------------------------------------------------------------------------------------------
Function CheckSql(Str)
 If Str = "" Then 
  CheckSql = "" 
  Exit Function  
 End If 
 Str = Replace(Str,Chr(0),"", 1, -1, 1) 
 Str = Replace(Str, """", "&quot;", 1, -1, 1) 
 Str = Replace(Str,"<","&lt;", 1, -1, 1) 
 Str = Replace(Str,">","&gt;", 1, -1, 1)  
 Str = Replace(Str, "script", "&#115;cript", 1, -1, 0) 
 Str = Replace(Str, "SCRIPT", "&#083;CRIPT", 1, -1, 0) 
 Str = Replace(Str, "Script", "&#083;cript", 1, -1, 0) 
 Str = Replace(Str, "script", "&#083;cript", 1, -1, 1) 
 Str = Replace(Str, "object", "&#111;bject", 1, -1, 0) 
 Str = Replace(Str, "OBJECT", "&#079;BJECT", 1, -1, 0) 
 Str = Replace(Str, "Object", "&#079;bject", 1, -1, 0) 
 Str = Replace(Str, "object", "&#079;bject", 1, -1, 1) 
 Str = Replace(Str, "applet", "&#097;pplet", 1, -1, 0) 
 Str = Replace(Str, "APPLET", "&#065;PPLET", 1, -1, 0) 
 Str = Replace(Str, "Applet", "&#065;pplet", 1, -1, 0) 
 Str = Replace(Str, "applet", "&#065;pplet", 1, -1, 1) 
 Str = Replace(Str, "[", "&#091;") 
 Str = Replace(Str, "]", "&#093;") 
 Str = Replace(Str, """", "", 1, -1, 1) 
 Str = Replace(Str, "=", "&#061;", 1, -1, 1) 
 Str = Replace(Str, "’", "’’", 1, -1, 1) 
 Str = Replace(Str, "select", "sel&#101;ct", 1, -1, 1) 
 Str = Replace(Str, "execute", "&#101xecute", 1, -1, 1) 
 Str = Replace(Str, "exec", "&#101xec", 1, -1, 1) 
 Str = Replace(Str, "join", "jo&#105;n", 1, -1, 1) 
 Str = Replace(Str, "union", "un&#105;on", 1, -1, 1) 
 Str = Replace(Str, "where", "wh&#101;re", 1, -1, 1) 
 Str = Replace(Str, "insert", "ins&#101;rt", 1, -1, 1) 
 Str = Replace(Str, "delete", "del&#101;te", 1, -1, 1) 
 Str = Replace(Str, "update", "up&#100;ate", 1, -1, 1) 
 Str = Replace(Str, "like", "lik&#101;", 1, -1, 1) 
 Str = Replace(Str, "drop", "dro&#112;", 1, -1, 1) 
 Str = Replace(Str, "create", "cr&#101;ate", 1, -1, 1) 
 Str = Replace(Str, "rename", "ren&#097;me", 1, -1, 1) 
 Str = Replace(Str, "count", "co&#117;nt", 1, -1, 1) 
 Str = Replace(Str, "chr", "c&#104;r", 1, -1, 1) 
 Str = Replace(Str, "mid", "m&#105;d", 1, -1, 1) 
 Str = Replace(Str, "truncate", "trunc&#097;te", 1, -1, 1) 
 Str = Replace(Str, "nchar", "nch&#097;r", 1, -1, 1) 
 Str = Replace(Str, "char", "ch&#097;r", 1, -1, 1) 
 Str = Replace(Str, "alter", "alt&#101;r", 1, -1, 1) 
 Str = Replace(Str, "cast", "ca&#115;t", 1, -1, 1) 
 Str = Replace(Str, "exists", "e&#120;ists", 1, -1, 1) 
 Str = Replace(Str,Chr(13),"<br>", 1, -1, 1) 
 CheckSql = Replace(Str,"’","’’", 1, -1, 1) 
End Function

 


'##############时间转换###################
Function gettime(s_second)
    ss = s_second Mod 60
  
    sm = ((s_second - ss) / 60) mod 60
    sh = (s_second - (sm * 60 + ss)) / (60 * 60)
    'gettime = CDate(sh & ":" & sm & ":" & ss)
 'gettime=AddZero(sm,2) & ":" & AddZero(ss,2)
 
 if left(ss,1)<>0 and len(ss)=1 then
  ss="0"&ss
 end if
 
 if left(sm,1)<>0 and len(sm)=1 then
  sm="0"&sm
 end if
 
 gettime=sh & "小时" & sm & "分钟"
End Function

 

'---------------------------------------------------------------------------------------------------------------------------------------
'功能:连接路径
'参数:Str1,Str2,路径
'说明:
'时间:
'作者:
'---------------------------------------------------------------------------------------------------------------------------------------
Function JoinPath(Str1,Str2)
 If Right(Str1,1) = "/" Or Right(Str1,1) = "/" Then
  Str1 = Left(Str1,Len(Str1)-1)
 End If
 If Left(Str2,1) = "/" Or Left(Str2,1) = "/" Then
  Str2 = Right(Str2,Len(Str2)-1)
 End If
 If Str1 = "" Then
  JoinPath = Str2
 ElseIf Str2 = "" Then
  JoinPath = Str1
 Else
  JoinPath = Str1 & "/" & Str2
 End IF
End Function

'---------------------------------------------------------------------------------------------------------------------------------------
'功能:格式化时间函数
'参数:
'说明:
'时间:
'作者:
'---------------------------------------------------------------------------------------------------------------------------------------
Function Formatdate3(dateval ,flag)
 Dim tempdate,temparray,yyyyhhdd,flagarray,tmp,timei,yyyy,mmstr,mm,ddstr,dd,returnsrt
 If isempty(dateval) or isnull(dateval) Then
   returnsrt=""
 Else
   tempdate=FormatDateTime(dateval, 2)&" "&FormatDateTime(dateval, 4)
   temparray=split( tempdate ," ")
   yyyyhhdd=temparray(0)

   flagarray=split(flag,"-")
   tmp=split(yyyyhhdd,"-")
   For timei=0 to ubound(flagarray)
      If flagarray(timei)="yy" Then
      yyyy=mid(tmp(0),3)
      ElseIf flagarray(timei)="yyyy" Then
      yyyy=tmp(0)
      ElseIf flagarray(timei)="mm" Then
      mmstr=""
      If tmp(1)+0<10 Then
        mmstr="0"
      End If
      mm=mmstr&tmp(1)
     ElseIf flagarray(timei)="m" Then
       mm=tmp(1)
     ElseIf flagarray(timei)="dd" Then
     ddstr=""
     If tmp(2)+0<10 Then
     ddstr="0"
     End If
     dd=ddstr&tmp(2)
     ElseIf flagarray(timei)="d" Then
       dd=tmp(2)
     Else
       ddstr=""
     If tmp(2)+0<10 Then
     ddstr="0"
     End If
       dd= ddstr&tmp(2)&" " &temparray(1)
      End If
   Next
    returnsrt=yyyy&"-"&mm&"-"&dd
  If yyyy="" Then
   returnsrt=mm&"-"&dd
  End If
  If mm="" Then
   returnsrt=dd
  End If
 End If
 Formatdate3=returnsrt
End Function

 


'#################截取字符################
function getLen(str,lenght)
 jj=0
 newstr=""
 for ii=1 to len(str)
  a=mid(str,ii,1)
  s=asc(a)
  newstr=newstr & a
  if s>0 then
    jj=jj+1  
  else
    jj=jj+2
  end if
  if jj>lenght and jj<len(str) then
   newstr=newstr & ".."
   exit for
  end if
 next
 getLen=newstr
end function

 

'##########################记录整个后台的编辑、删除、增加的操作##################################
'###  username:被操作人的BBS_ID                                                             ####
'###  admin:操作人的BBS_ID                                                                  ####
'###  doing:操作                                                                            ####
'###  pageid:操作页面ID                                                                     ####
'###  datenow:操作时间                                                                      ####
'###  other:其他记录                                                                        ####
'################################################################################################
function savelog(message,pageid,other)
 conn.execute("insert into [log_frezon] (admin,doing,pageid,datenow,other) values ('"&session("username")&"','"&message&"','"&pageid&"','"&now&"','"&other&"')")
end function

 

'##############通用存储过程调用##################
'sTables       表名
'sPK           主键
'sSort         排序字段  
'sPageNumber   当前页数  
'sPageSize     总页数
'sTotalCount    总记录
'sTotalPage     总页数
'sFields       返回的字段  
'sFilter       查询条件  
'sGroup        按组分
'sConn         数据库连接 
'##############通用存储过程调用##################
function Read_Pro(sTables,sPK,sSort,sPageNumber,sPageSize,sTotalCount,sTotalPage,sFields,sFilter,sGroup,sConn)

 adVarChar = 200
 adParamInput = 1
 adCmdStoredProc=4
 adInteger = 3
 adTinyInt = 16

 Set cmdTest = Server.CreateObject("ADODB.Command")
 
 cmdTest.CommandText = "pro_Paging"
 
 cmdTest.CommandType = adCmdStoredProc
 
 Set prmTest=cmdTest.CreateParameter("Tables",adVarChar,adParamInput,225,sTables)
 cmdTest.Parameters.Append prmTest
 
 Set prmTest=cmdTest.CreateParameter("PK",adVarChar,adParamInput,225,sPK)
 cmdTest.Parameters.Append prmTest 
 
 Set prmTest=cmdTest.CreateParameter("Sort",adVarChar,adParamInput,255,sSort)
 cmdTest.Parameters.Append prmTest
 
 Set prmTest=cmdTest.CreateParameter("PageNumber",adInteger,adParamInput,,sPageNumber)
 cmdTest.Parameters.Append prmTest
 
 Set prmTest=cmdTest.CreateParameter("PageSize",adInteger,adParamInput,,sPageSize)
 cmdTest.Parameters.Append prmTest
 
 Set prmTest=cmdTest.CreateParameter("TotalCount",adInteger,adParamOutput,,sTotalCount)
 cmdTest.Parameters.Append prmTest
 
 Set prmTest=cmdTest.CreateParameter("TotalPage",adInteger,adParamOutput,,sTotalPage)
 cmdTest.Parameters.Append prmTest
 
 Set prmTest=cmdTest.CreateParameter("Fields",adVarChar,adParamInput,1000,sFields)
 cmdTest.Parameters.Append prmTest
 
 Set prmTest=cmdTest.CreateParameter("Filter",adVarChar,adParamInput,1000,sFilter)
 cmdTest.Parameters.Append prmTest
 
 Set prmTest=cmdTest.CreateParameter("Group",adVarChar,adParamInput,255,sGroup)
 cmdTest.Parameters.Append prmTest
  
 Set cmdTest.ActiveConnection = sConn
 set rrs= cmdTest.Execute()
 Read_Pro=rrs
end function

 

分页

<table width="97%"  border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td><div align="center">第 <font color=red><%=cstr(npage)%></font> 页 / 共 <font color=red><%=cstr(rs.pagecount)%></font> 页 一共有 <font color=red><%=cstr(rs.recordcount)%></font> 条记录  
            <%
      if npage=1 then
        response.write "首页 上页"
      else
      %>
                    <a href="?&page=1&typeid=2">首页</a> <a href="?page=<%=cstr(npage-1)%>&typeid=2">上页</a>
                    <%end if%>
                    <%
      if npage>=cdbl(rs.pagecount) then
        response.write " 下页 末页"
      else
      %>
            <a href="?page=<%=cstr(npage+1)%>&typeid=2">下页</a> <a href="?page=<%=cstr(rs.pagecount)%>&typeid=2">末页</a>
            <%end if%>
          </div></td>
        </tr>
    </table>