ASP excellent questions and answers (3)

Source: Internet
Author: User

1. Prevent users from accessing the page directly. Prevent Self-made forms from being submitted and directly accessing the page through links)
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 ("Please access this website through proper methods ")
Response. End ()
End if
End function
2. The following two functions can be used to submit images and texts simultaneously.
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 (partition B (formdata, bncrlf)-1)
Datastart = instrB (formdata, bncrlf & bncrlf) + 4
Dataend = Nation B (datastart + 1, formdata, divider)-datastart-2
Mydata (I-1) = midB (formdata, datastart, dataend)
Formdata = rightB (formdata, clng (formsize-Snapshot B (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 = "submit">
</Form>
B. asp:
'Link to the database
Data = gainfromdata (2)
Rs ("txt") = binarytostring (data (0 ))
Rs ("img"). appendchunk = data (1)

3. A prompt is displayed to confirm and cancel the operation.
Onclick = "{if (confirm ('Are you sure you want to delete the selected record? '{%%This.doc ument. inbox. submit (); return true ;}return false ;}"
4. Optimization of combined queries. Thank you.
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"
Combined Query has four combination conditions. is writing efficiency low? How can I optimize it? Thank you.
Analysis: in SQL, using LIKE is time-consuming, so it is best to use less. At the same time, in the four TEXT boxes, the input condition is: when there are no input conditions, there will be LIKE "%". In fact, in this case, this condition is not.
Therefore, write more code for generating SQL statements,
Dim WhereStr as string
If txtbname. Text <> "" then
WhereStr = "bname like '%" & txtbname. Text & "% '"
Else if ....
.....
ENDIF
This is a probability problem. If four full inputs are the same, but if only one input is used, the speed will be faster than yours!
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. How to disable refresh
<Script language = "JavaScript">
Document. onkeydown = function (){
If (event. keyCode = 116 ){
Event. keyCode = 0;
Event. returnValue = false;
}
}
Document. oncontextmenu = function () {event. returnValue = false ;}
</SCRIPT>
Page cannot be refreshed

6. SQL server uses ql = "update reg set dealtime = getdate () where id =" & request. querystring ("id ")
Because SQL SERVER does not have the now () function, it replaces it with getdate ().
So you will report an error.
ACCESS does not have this function
Only date () And now ()
7. How to connect other databases (*. dbf, *. txt, excel, and foxpro)
18:41:05 views: 145
'Link the dbf File
<%
'Create a Connection object
Set conn = Server. CreateObject ("ADODB. Connection ")
Driver = "Driver = {Microsoft Visual FoxPro Driver };"
SourceType = "SourceType = DBF ;"
DBPath = "SourceDB =" & Server. MapPath ("Dbf ")
'Call the Open Method to connect to the database
Conn. Open Driver & SourceType & DBPath
Set rs = Server. CreateObject ("ADODB. Recordset ")
'Open the data source. Parameter 2 is the Connection object.
Rs. Open "Select * From sample", conn, 2, 2
%>
'Link The foxpro File
<%
'Create a Connection object
Set conn = Server. CreateObject ("ADODB. Connection ")
Driver = "Driver = {Microsoft Visual FoxPro Driver };"
SourceType = "SourceType = DBC ;"
DBPath = "SourceDB =" & Server. MapPath ("Dbf/Sample. dbc ")
'Call the Open Method to connect to the database
Conn. Open Driver & SourceType & DBPath
Set rs = Server. CreateObject ("ADODB. Recordset ")
'Open the data source. Parameter 2 is the Connection object.
Rs. Open "Select * From sample", conn, 2, 2
%>
'Link an excel file
<%
'Create a Connection object
Set conn = Server. CreateObject ("ADODB. Connection ")
Driver = "Driver = {Microsoft Excel Driver (*. xls )};"
DBPath = "DBQ =" & Server. MapPath ("Sample.xls ")
'Call the Open Method to connect to the database
Conn. Open Driver & DBPath
Set rs = Server. CreateObject ("ADODB. Recordset ")
'Open the data source. Parameter 2 is the Connection object.
Rs. Open "Select * From [Transcript $]", conn, 2, 2
%>
'Link the txt file
<%
'Create a Connection object
Set conn = Server. CreateObject ("ADODB. Connection ")
Driver = "Driver = {Microsoft Text Driver (*. txt; *. csv )};"
DBPath = "DBQ =" & Server. MapPath ("Text ")
'Call the Open Method to connect to the database
Conn. Open Driver & DBPath
Set rs = Server. CreateObject ("ADODB. Recordset ")
'Open the data source. Parameter 2 is the Connection object.
Rs. Open "Select * From sample.txt", conn, 2, 2
%>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.