ASP常见问题及解答(6)

发表于:2007-06-30来源:作者:点击数: 标签:
1.防止用户直接访问页面(防止自制表单提交,直接通过链接访问) function checkPrePage() url=request.ServerVariables(HTTP_REFERER) url=trim(replace(url,http://,)) url=trim(left(url,len(request.ServerVariables(SERVER_NAME)))) if urltrim(request.
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
%>

原文转自:http://www.ltesting.net