Some of the ASP learning notes recorded

Source: Internet
Author: User
Tags date format array empty sql numeric value square root access
Simple counters made with application and session notes
<%
If IsEmpty (Session ("Conn")) Then
Application.Lock
Set Counterfile=server.createobject ("Scripting.FileSystemObject")
Set Temp=counterfile.opentextfile (Server.MapPath ("Count.txt"), 1,true,false)
Num=temp.readline
If Num=empty then Num=1
Application ("Count") =num
Application ("Count") =application ("Count") +1
Temp.close
End If
%>
<center><%
Set Counterfile=server.createobject ("Scripting.FileSystemObject")
Set Temp=counterfile.createtextfile (Server.MapPath ("Count.txt"), True,false)
Temp.writeline (Application ("Count")
Temp.close
Application.UnLock
%>

###########################################################################

If Then program statement End If
<%
Score=request ("Score")
If not IsNumeric (score) Then
m= "Please enter a number"
ElseIf score= "" Then
m= "Please enter data"
ElseIf score>=60 and score<=100 Then
M= "Pass."
ElseIf score>100 or Score<0 Then
m= "Please enter the number between 0---100"
Else
M= "failed."
End If
%>

Cookies

<%
Response.buffer=true ' because it was not exchanged before the server downloaded the data, so use the buffer to load the downloaded data
Var=request.cookies ("var")
If var= "" then Var=empty ' because "" cannot be numeric operation
Response.Write "Start var=" &var& "<br>"
Var=var+10
Response.Write "End var=" &var& "<br>"
Response.Cookies ("var") =var
Response.Cookies ("va"). expires= "2004/3/31/" set the validity period
%>

###########################################################################

Moving, copying, and deleting files

<%
Set Fs=server.createobject ("Scripting.FileSystemObject")
Source=server.mappath ("1/1.txt")
Target=server.mappath ("1.txt")
On Error Resume Next
Fs.movefile source,target ' MoveFile in Deletefile\copyfile
If Err.number=53 Then
Response.Write source& "file does not exist"
Response.End
ElseIf Err.number=70 Then
Response.Write target& "File is read-only or locked"
Response.End
ElseIf Err.nuber<>0 Then
Response.Write "Other=" &err.number
Response.End
End If
%>

##########################################################

Reading text content
<%
Set Fs=server.createobject ("Scripting.FileSystemObject")
File=server.mappath ("1/1.txt")
Set Txt=fs.opentextfile (file,1,true)
If not Txt.atendofstream then
Line=txt.readline ' can be replaced by ReadAll
Response.Write line& "<br>"
End If
%>

############################################

SERVER & Request]
<%=server.htmlencode ("<i> Original code output </i>")%>
<%=request.servervariables ("local_addr")%> ' local IP
<%=request.servervariables ("remote_addr")%> ' client-side IP

Two ways to connect to an Access database:
1, <% Set CN = Server.CreateObject ("ADODB.") Connection "
CN. Open "Driver={microsoft Access Driver (*.mdb)};D bq=" &server.mappath ("Message.mdb")
Set rs=server.createobject ("ADODB. Recordset ")
%>
2, <% Set con = Server.CreateObject ("ADODB. Connection ")
DBPath = Server.MapPath ("Bbs.mdb")
Con. Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &dbpath
Set rs=server.createobject ("ADODB. Recordset ")
%>

##########################################################################

Several ways to open data tables
1. SQL = "SELECT * from data table Where Main Number =" & Request ("ID")
Rs. Open sql,con,1,3
2, RS. Open "Datasheet", con,1,3
3, RS. Open "SELECT * from student", con,1,3

Aspects of reading database data

1, <%=rs (0) .name%> ' read header name of first column
2, <%=rs (1) .value%> ' Read the contents of the second column
3, <%=rs.fields (1). Value%> ' ditto
4, Rs.recordcount ' total record number
5, Rs.fields.Count ' has several fields

###########################################################################
such as reading data from a table:
<%
Num=0
For itemline=0 to Rs.recordcount-1 ' recordcount-1 does not move the last record
Response.Write "<TR>"
For I=0 to Rs.fields.count-1
Response.Write "<td><input type=text name=" & Num & _
"Value=" & Rs (I). Value & "></TR>"
Response.Write Chr (10) ' output line feed
Num=num+1
Next

Response.Write "</TR>"
Rs.movenext
If rs.eof Then Exit for ' can be omitted
Next
%>

Save data file
###########################################################################
<%
How much of the Dim Item (100) ' array is important
For I=0 to 100
Item (i) =request (i)
Next
Set conobject = Server.CreateObject ("ADODB. Connection ")
DBPath = Server.MapPath ("Exam.mdb")
Conobject. Open "Driver={microsoft Access Driver (*.mdb)};D bq=" &dbpath
Set rs=server.createobject ("ADODB. Recordset ")
Rs. Open "Select * from", conobject,1,3
Num=0
For Itemline=0 to Rs.recordcount-1
For Itemnum=1 to Rs.fields.count-1
Rs.fields (Itemnum). Value=item (Num)
Num=num+1
Rs.update
Next
Rs.movenext
If rs.eof Then Exit for
Next
Response.Redirect "Editexam.asp"

%>

###########################################################################
Determine whether the form is complete
1, If name = "" or email = "" or subject = "" Or _
Textmemo = "" Then
Response.Write "<script>alert (' pine Nut said, do not fill out the message!"); History.go ( -1);</script> "
Response.End
End If

Adding new data methods
1, Rs.Open "" &s& "", con,1,3
Rs.addnew
RS ("name") =request ("name")
RS ("Depa") =request ("Depa")
Rs.update
%>

##############################################################

Common functions
One, character class
1, ASC (string) ' This value is the ASCII value of the first letter of the parameter string such as ASC (a) =97
2, Chr (charcode) ' Converts the specified character code to a character (string) such as CHR (97) equals a
3, Len (string) ' The number of characters in the string in the calculation parameter is as Len (ab C) =4
4, left (string, specified length) as left ("123 4", 3) =123
5, right Ditto
6, Mid (string, start character, specified length) such as Mid ("123 4", 3, 1) =1
7, LTrim (string) ' Removes the front blank in the string such as LTrim ("hello") = "Hello"
8, RTrim (string) ditto
9, Trim (String) ' Remove the blank before and after the string
10, UCase (string) converts all characters in a string to uppercase letters such as UCase ("h") = "h"
11, Lcase (string) ditto
12, space (number of spaces) such as "Hello" &space (1) & "Jacky" =hello Jacky
13, String (character number, character) is used to produce a repeating character strings such as String (3, "AB") =aaa ' two characters only repeat the first
14, Join (source array [, separator character])
15, Split (source string, delimited string)
16. Replace (source string, character in source string, string to be changed)
such as a= "Long" Response.Write (replace (A, "L", "s")) =song
Second, the Mathematics class
1, Abs (value) ' Returns the absolute value of the parameter values, the type of the return value is the same as the parameter numeric type.
2, SGN (value) to determine the number of parameters, the parameter is positive, return 1, negative return -1,0 return 0
3, SQR (numerical) to obtain the square root of the value, can not be negative
4, Hex (numerical) conversion to 16 carry
5, OCT (numerical) conversion to 8 carry
Iii. Category Conversions
1, int (numeric) returns the integer part of the value such as int (3,3) =3
2, fix (numerical) ibid, just int (-3.8) =4 and fix (-3.8) =3
3, STR (numeric) Converts a numeric value to a string.
4, Format (processing objects [, format])
5, year (date)/month (date)/day (date)
6, Hour (time)/minute (time)/second
7, WeekdayName (1-7) such as WeekdayName (1) = Sunday
8, MonthName (1-12) such as monthname (1) = month
Iv. comparison operators: =, <>, <, >, <=, >=
Five, not logical operators: and, OR, not
VI. Specify the operation symbol: =
Seven, operator symbol: +, *,/, \ (integer division) 2\5=0, Mod (division to remainder), ^ multiply (second side) 5^2=25
The string-pull operator:& such as ABC&AMP;EFG=ABCDEFG

To delete a record from a table:
1, <a href=del.asp?id=<%=rs ("ID")%> > Delete </a>
2, <%
Id=request ("id")
Set CN = Server.CreateObject ("ADODB.") Connection ")
CN. Open "Driver={microsoft Access Driver (*.mdb)};D bq=" &server.mappath ("New.mdb")
Set rs=server.createobject ("ADODB. Recordset ")
Rs. Open "New", cn,1,3
Sql= "Delete * from new where id=" &id
Cn.execute SQL
Response.Redirect ("index.asp")
%>

###########################################################################
To keep the text box in a consistent format:
<%
function th (str)
Str=replace (str, "damn", "MD")
Str=replace (str, "lean", "KAO")
Th=str
End Function

function encode (str)
str = replace (str, ">", ">")
str = replace (str, "<", "<")
str = Replace (str, CHR (32), "")
str = Replace (str, CHR (13), "")
str = Replace (str, CHR (a) & CHR (a), "</P><P>")
str = Replace (str, CHR (Ten), "<BR>")
Encode=str
End Function
%>

<%title=request.form ("title")
Content=request.form ("content")%>

Article title: <%=th (title)%>Article content: <%=encode (content)%>


###########################################################################
Skills:
1. Response.Write Chr (10) ' Output a newline symbol
2, the filename of this page <%=request.servervariables ("Path_info")%>
3, with response
<%
With response
. Write "This"
. Write "is"
. Write "A"
. Write "Test"
End With
%>

Related Article

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.