欢迎光临
我们一直在努力

ASP常见问题及解答(6)-ASP教程,ASP技巧

建站超值云服务器,限时71元/月

1.防止用户直接访问页面(防止自制表单提交,直接通过链接访问)

function checkprepage()

url=request.servervariables("http_referer")

url=trim(replace(url,"http://",""))

url=trim(left(url,len(request.servervariables("server_name"))))

if url<>trim(request.servervariables("server_name")) then

response.write("请通过正当的方法访问本网站")

response.end()

end if

end function

2.通过一下的两个函数可以实现图片,文字的同时提交处理。

function binarytostring(str)

strto = ""

for i=1 to lenb(str)

if ascb(midb(str, i, 1)) > 127 then

strto = strto & chr(ascb(midb(str, i, 1))*256+ascb(midb(str, i+1, 1)))

i = i + 1

else

strto = strto & chr(ascb(midb(str, i, 1)))

end if

next

binarytostring=strto

end function

function gainformdata(n)

dim formsize,formdata,divider,datastart,dataend

redim mydata(n-1)

formsize = request.totalbytes

formdata = request.binaryread(formsize)

for i=1 to n

bncrlf = chrb(13) & chrb(10)

divider = leftb(formdata,clng(instrb(formdata,bncrlf))-1)

datastart = instrb(formdata,bncrlf & bncrlf)+4

dataend = instrb(datastart+1,formdata,divider) – datastart-2

mydata(i-1) = midb(formdata,datastart,dataend)

formdata=rightb(formdata,clng(formsize-instrb(datastart+1,formdata,divider))+1)

formsize=lenb(formdata)

next

gainformdata=mydata

end function

demo:

a.htm:

<form name="form1" method="post" action="b.asp" enctype="multipart/form-data">

<textarea name="txt"></textarea>

<input type="file" name="file">

<input type="submit" name="submit" value="提交">

</form>

b.asp:

链接数据库

data=gainfromdata(2)

rs("txt")=binarytostring(data(0))

rs("img").appendchunk=data(1)

3.弹出提示信息 , 确定 与 取消 怎么做的

onclick="{if(confirm(确定删除选定的纪录吗?)){this.document.inbox.submit();return true;}return false;}"

4.组合查询的优化,谢谢

sql = "select * from book where bname like %" & txtbname.text & "% and bauthor like %" & txtauthor.text & "% and bpublish like %" & txtpublish.text & "% and bdescription like %" & txtdescription.text & "% order by bookid desc"

组合查询,有是四个组合条件,这样写是不是效率比较低,我该怎么优化一下呢?

谢谢。

分析,在sql中,用like是比较费时间的,所以最好是少用。

同时 ,四个text框,输入条件是,一定会有没有输入的条件的时候,就会出现 like "%"的情况,其实这种情况下就等于这个条件没有。

所以,把生成sql语句的代码多写些,

dim wherestr as string

if txtbname.text<>"" then

wherestr="bname like %" & txtbname.text & "%"

else if ….

…..

endif

这样是一个概率的问题,如果四个全输入的话,是一样的,但如果只输入一个的问,速度会比你的快些!

sql = "select * from book where "

if txtbname.text <> "" then

sql = sql & "bname like %" & txtbname.text & "%"

elseif txtauthor.text <> "" then

sql = sql & "bauthor like %" & txtauthor.text & "%"

elseif txtpublish.text <> "" then

sql = sql & "bpublish like %" & txtpublish.text & "%"

elseif txtdescription.text <> "" then

sql = sql & "bdescription like %" & txtdescription.text & "% "

end if

5.如何禁止刷新

<script language="javascript">

document.onkeydown = function() {

if(event.keycode==116) {

event.keycode=0;

event.returnvalue = false;

}

}

document.oncontextmenu = function() {event.returnvalue = false;}

</script>

页面已经禁止刷新

6.sql server 用:sql = "update reg set dealtime=getdate() where id= " & request.querystring("id")

因为 sql server里没有 now() 这个函数,而是用 getdate() 取代了

所以你的会报错.

access没有这个函数:)

只有date()和now()

7.连结其他数据库的方法(*.dbf,*.txt,excel,foxpro等) —-收藏

2002-10-30 18:41:05 浏览次数:145

连结dbf文件

<%

建立connection 对象

set conn = server.createobject("adodb.connection")

driver = "driver={microsoft visual foxpro driver};"

sourcetype = "sourcetype=dbf;"

dbpath = "sourcedb=" & server.mappath( "dbf" )

调用open 方法连接数据库

conn.open driver & sourcetype & dbpath

set rs = server.createobject("adodb.recordset")

打开数据源,参数二为connection对象

rs.open "select * from sample", conn, 2, 2

%>

连结foxpro文件

<%

建立connection 对象

set conn = server.createobject("adodb.connection")

driver = "driver={microsoft visual foxpro driver};"

sourcetype = "sourcetype=dbc;"

dbpath = "sourcedb=" & server.mappath( "dbf/sample.dbc" )

调用open 方法连接数据库

conn.open driver & sourcetype & dbpath

set rs = server.createobject("adodb.recordset")

打开数据源,参数二为connection对象

rs.open "select * from sample", conn, 2, 2

%>

连结excel文件

<%

建立connection对象

set conn = server.createobject("adodb.connection")

driver = "driver={microsoft excel driver (*.xls)};"

dbpath = "dbq=" & server.mappath( "sample.xls" )

调用open 方法连接数据库

conn.open driver & dbpath

set rs = server.createobject("adodb.recordset")

打开数据源,参数二为connection对象

rs.open "select * from [成绩单$]", conn, 2, 2

%>

连结txt文件

<%

建立connection 对象

set conn = server.createobject("adodb.connection")

driver = "driver={microsoft text driver (*.txt; *.csv)};"

dbpath = "dbq=" & server.mappath( "text" )

调用open 方法连接数据库

conn.open driver & dbpath

set rs = server.createobject("adodb.recordset")

打开数据源,参数二为connection对象

rs.open "select * from sample.txt", conn, 2, 2

%>

8.画图:-)

<object id=structuredgraphicscontrol1 style="left: 0px; width: 392px; top: 0px; height: 240px"

height=240 width=392 classid="clsid:369303c2-d7ac-11d0-89d5-00a0c90833e6">

</object>

<p>&nbsp;</p>

<script language=vbscript>

structuredgraphicscontrol1.drawingsurface.arcdegrees 0,0,0,30,50,60

structuredgraphicscontrol1.drawingsurface.arcradians 30,0,0,30,50,60

structuredgraphicscontrol1.drawingsurface.line 10,10,100,100

</script>

9.生成excel

<!– #include file=../inc/connect.asp –>

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=gb2312">

<title>下载excel文件</title>

</head>

<body>

<%

set rs=server.createobject("adodb.recordset")

dbopen

sql=request("sql")

sql=replace(sql,"|百分号|",ucase("%"))

title=request("title")

response.write sql & "<br/>"

rs.open sql,conn,3,2

set xlapp=server.createobject("excel.application")

xlapp.visible = false

set mybook=xlapp.workbooks.add

set mysheet=mybook.worksheets(1)

myarray=split(title,"|")

增加表头

for i=0 to ubound(myarray)-1

rangex=ucase(chr(65+i))

mysheet.range(rangex & 1 ).value=cstr(myarray(i))

next

set myarray=nothing

if rs.recordcount>0 then

j=1

do while not rs.eof

j=j+1行

for i=0 to rs.fields.count-1

if i<26 then

rangex=ucase(chr(65+i))

response.write rangex & j & "<br/>"

if not isnull(rs.fields(i).value) then mysheet.range(rangex & j ).value=cstr(rs.fields(i).value)

end if

next

rs.movenext

loop

end if

response.write rs.fields.count & "<br/>"

response.write rs.recordcount & "<br/>"

randomize

myfilename=session("userrealname") & date() & "-" & cint(rnd *10000) & ".xls"

mypath=server.mappath("excel.asp")

myarray=split(mypath,"\")

mypath=""

for i=0 to ubound(myarray)-1

mypath=mypath & myarray(i) & "\"

next

response.write mypath & myfilename

mybook.saveas(mypath & myfilename)

mybook.close

xlapp.quit

set mysheet=nothing

set mybook=nothing

set xlapp=nothing

%>

<img src="../i/d_wealth_out.gif" width="16" height="16"><a name="download" href="<%="download.asp?filename=" & myfilename%>">下载

<%=myfilename%></a>

</body>

</html>

10.下载任何文件(尤其是ie关联打开的)

<%

dim stream

dim contents

dim filename

dim fileext

const adtypebinary = 1

filename = request.querystring("filename")

if filename = "" then

response.write "无效文件名."

response.end

end if

下面是不希望下载的文件

fileext = mid(filename, instrrev(filename, ".") + 1)

select case ucase(fileext)

case "asp", "asa", "aspx", "asax", "mdb"

response.write "受保护文件,不能下载."

response.end

end select

下载这个文件

response.clear

response.contenttype = "application/octet-stream"

response.addheader "content-disposition", "attachment; filename=" & filename

set stream = server.createobject("adodb.stream")

stream.type = adtypebinary

stream.open

stream.loadfromfile server.mappath(filename)

while not stream.eos

response.binarywrite stream.read(1024 * 64)

wend

stream.close

set stream = nothing

response.flush

response.end

%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP常见问题及解答(6)-ASP教程,ASP技巧
分享到: 更多 (0)

相关推荐

  • 暂无文章