在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
8条Asp编码优化技巧: 1、声明VBScript变量 2、对URL地址进行编码 HTTP Error 400 400 Bad Request Due to malformed syntax, the request could not be understood by the server. The client should not repeat the request without modifications. 解决方法是对生成的URL参数使用ASP内置server对象的URLencode方法进行URL编码,例子如下: <% URL="xur.asp" var1="username=" & server.URLencode("xur") var2="&company=" & server.URLencode("xurstudio") var3="&phone=" & server.URLencode("021-53854336-186") response.redirect URL & "?" & var1 & var2 & var3 %> 3、清空对象 <% myDSN="DSN=xur;uid=xur;pwd=xur" mySQL="select * from authors where AU_ID<100" set conntemp=server.createobject("adodb.connection") conntemp.open myDSN set rstemp=conntemp.execute(mySQL) if rstemp.eof then response.write "数据库为空" response.write mySQL conntemp.close set conntemp=nothing response.end end if%> <%do until rstemp.eof %> <% rstemp.movenext loop rstemp.close set rstemp=nothing conntemp.close set conntemp=nothing %> 4、使用字符串建立SQL查询 <%mySQL= ""select * " mySQL= mySQL & "from publishers" mySQL= mySQL & "where state='NY'" response.write mySQL set rstemp=conntemp.execute(mySQL) rstemp.close set rstemp=nothing %> 5、使用case进行条件选择 <% FOR i = 1 TO 1000 n = i Response.Write AddSuffix(n) & "<br>" NEXT %> <% Function AddSuffix(num) numpart = RIGHT(num,1) SELECT CASE numpart CASE "1" IF InStr(num,"11") THEN num = num & "th" ELSE num = num & "st" END IF CASE "2" IF InStr(num,"12") THEN num = num & "th" ELSE num = num & "nd" END IF CASE "3" IF InStr(num,"13") THEN num = num & "th" ELSE num = num & "rd" END IF CASE "4" num = num & "th" CASE ELSE num = num & "th" END SELECT AddSuffix = num END FUNCTION %> 6、使用adovbs.inc文件中定义的常量打开记录集 <!--#INCLUDE VIRTUAL="/ADOVBS.INC" --> <% connectme="DSN=xur;uid=xur;pwd=xur" sqltemp="select * from publishers where name='xur'" set rstemp=Server.CreateObject("adodb.Recordset") rstemp.open sqltemp, connectme, adOpenStatic,adLockOptimstic response.write rstemp.recordcount & " records in<br>" & sqltemp rstemp.close set rstemp=nothing %> 7、避免在使用global.asa文件中进行对象定义 <%SUB application_onstart set application("theCONN")=server.createobject("adodb.connection") END SUB %>; 这样就可以在站点任何代码中做类似引用: <% mySQL="select * from publishers where state='xur' set rstemp=application("theconn").execute(mySQL) %> 同样地,可以在session_onstart函数中创建记录集对象 <%SUB session_onstart set session("rstemp")=server.createobject("adodb.recordset") END SUB %> 然后在站点也面中进行如下引用: <% mySQL="select * from publishers where state='xur' set session("rstemp")=conntemp.execute(mySQL) %> 但这样做的同时也有很大的负面影响,由于Application和session变量都只有在关闭网站的时候才释放占用的资源,所以session参数会浪费大量不必要内存,而且此时application变量成为服务器性能的瓶颈。 以上八条Asp编码优化技巧,每一条都很重要,需要大家细细体会,真正的理解成为自己的东西。 |
请发表评论