20 ASP items that beginners should remember
1. How to use ASP to determine the virtual physical path of your website
Answer: Use the Mappath method
< p align=center >< font size=4 face=Arial >< b >
The Physical path to this virtual website is:
< /b >< /font >
< font color=#FF0000 size=6 face=Arial >
< %= Server.MapPath(/)% >
< /font >< /p >
2. How do I know the browser the user is using?
Answer: Use the Request object method
strBrowser=Request.ServerVariables(HTTP_USER_AGENT)
If Instr(strBrowser,MSIE) < > 0 Then
Response.redirect(ForMSIEOnly.htm)
Else
Response.redirect(ForAll.htm)
End If
3. How to calculate the average number of repeat visitors per day
Answer: Solution
< % startdate=DateDiff(d,Now,01/01/1990)
if strdate< 0 then startdate=startdate*-1
avgvpd=Int((usercnt)/startdate) % >
Show results
< % response.write(avgvpd) % >
that is it.this page has been viewed since November 10,1998
4. How to display random images
< % dim p,ppic,dpic
ppic=12
randomize
p=Int((ppic*rnd)+1)
dpic=graphix/randompics/&p&.gif
%>
show
< img src=< %=dpic% > >
5.How to return to the previous page
Answer: < a href=< %=request.serverVariables(Http_REFERER)% > >preivous page< /a >
Or use a picture such as: < img src=arrowback.gif target='_blank'> If the server is running, if a dialog box can be displayed on the server, then you have to wait until someone clicks OK before your program can continue to execute. Generally, The server will not be guarded, so Microsoft has to ban this function and randomly tell you (:) haha) that you have no permission. However, the combination of ASP and client script can display a dialog box, as follows:
< % yourVar=Test Dialog % >
< % script language='javascript' >
alert(< %=yourvar% >)
</script>
11. Is there any way to protect your source code from being seen by others?
Answer: You can download a Microsoft Windows Script Encoder, which can encrypt asp scripts and client-side javascript/vbscript scripts. . . However, after the client is encrypted, only ie5 can execute it. After the server-side script is encrypted, it can only be executed if script engine 5 is installed on the server (just install an ie5).
12.How can I transfer query string from one asp file to another?
Answer: Add the following sentence to the former file: Response.Redirect(second.asp? & Request.ServerVariables(QUERY_STRING))
13.global.asa file always doesn’t work?
Answer: Global.asa is only valid if the web directory is set to web application, and global.asa is valid in the root directory of a web application. IIS4 can use Internet Service Manager to set application settings. How can I make the htm file execute script code like an asp file?
14. How can I make the htm file to execute script code like the asp file?
Answer: Internet Services Manager -> select default web site -> right mouse button -> menu properties -> home directory -> application settings (Application Setting) -> click button configuration -> app mapping -> click button Add -> executable Browse select /WINNT YSTEM32/INETSRV/ASP.DLL EXTENSION, enter htm method exclusions, enter PUT.DELETE, and confirm everything. However, it is worth noting that the htm will also be processed by asp.dll, and the efficiency will be reduced.
15.How to register components
Answer: There are two methods.
The first method: Manually register the DLL. This method has been used from IIs 3.0 to IIs 4.0 and other Web Servers. It requires you to execute it from the command line, enter the directory containing the DLL, and enter: regsvr32 component_name.dll, for example, c:/temp egsvr32 AspEmail.dll. It will register the specific information of the dll into the registry on the server. The component can then be used on the server, but there is a flaw in this approach. After the component is registered using this method, the component must set the NT anonymous account accordingly to have permission to execute the dll. In particular, some components need to read the registry, so this method of registering components is only used when there is no MTS on the server. To unregister the dll, use: regsvr32 /u aspobject.dll example c:/temp egsvr32 / uaneiodbc.dll
Second method: Use MTS (Microsoft Transaction Server) MTS is a new feature of IIS 4, but it provides huge improvements. MTS allows you to specify that only privileged users can access components, greatly improving the security settings on the website server. The steps to register a component on MTS are as follows:
1) Open the IIS management console.
2) Expand transaction server, right-click pkgs installed and select new package.
3) Click create an empty package.
4) Name the package.
5) Specify the administrator account or use interactive (if the server is often logged in using the administrator).
6) Now right-click on the expanded components under the package you just created. Select new then component.
7) Select install new component [b].
8) Find your .dll file and select next to finish.
To delete this object, just select its icon and then select delete.
Note: Pay special attention to the second method, it is the best way to debug the components you write without having to restart the machine every time.
16. ASP and Access database connection:
17. ASP and SQL database connection:
Create a recordset object:
set rs=server.createobject(adodb.recordset)
rs.open SQL statement,conn,3,2
18. How to use common SQL commands:
(1) Data record screening:
sql=select * from data table where field name = field value order by field name [desc]
sql=select * from data table where field name like '%field value%' order by field name [desc]
sql=select top 10 * from data table where field name order by field name [desc]
sql=select * from data table where field name in ('value 1', 'value 2', 'value 3')
sql=select * from data table where field name between value 1 and value 2
(2) Update data record:
sql=update data table set field name=field value where conditional expression
sql=update data table set field 1=value 1, field 2=value 2...field n=value n where conditional expression
(3) Delete data records:
sql=delete from data table where conditional expression
sql=delete from data table (delete all records in the data table)
(4) Add data record:
sql=insert into data table (field 1, field 2, field 3...) values (value 1, value 2, value 3...)
sql=insert into target data table select * from source data table (add records from the source data table to the target data table)
(5) Data recording statistical function:
AVG(field name) derives a table column average
COUNT(*|field name) counts the number of data rows or counts the number of data rows with a value in a certain column
MAX (field name) gets the maximum value of a table column
MIN (field name) gets the minimum value of a table column
SUM (field name) adds the values of the data columns
How to reference the above function:
sql=select sum(field name) as alias from data table where conditional expression
set rs=conn.excute(sql)
Use rs (alias) to obtain statistical values, and use the same methods as above for other functions.
(5) Creation and deletion of data tables:
CREATE TABLE data table name (field 1 type 1 (length), field 2 type 2 (length)...)
Example: CREATE TABLE tab01(name varchar(50),datetime default now())
DROP TABLE data table name (permanently delete a data table)
19. Methods of recordset object:
rs.movenext moves the record pointer down one line from the current position
rs.moveprevious moves the record pointer up one line from the current position
rs.movefirst moves the record pointer to the first row of the data table
rs.movelast moves the record pointer to the last row of the data table
rs.absoluteposition=N moves the record pointer to row N of the data table
rs.absolutepage=N moves the record pointer to the first row of page N
rs.pagesize=N sets each page to N records
rs.pagecount returns the total number of pages according to the settings of pagesize
rs.recordcount returns the total number of records
rs.bof returns whether the record pointer exceeds the beginning of the data table, true means yes, false means no
rs.eof returns whether the record pointer exceeds the end of the data table, true means yes, false means no
rs.delete deletes the current record, but the record pointer does not move downwards
rs.addnew adds records to the end of the data table
rs.update updates data table records
-----------------------------------------------------------
20 Recordset object methods
Open method
recordset.Open Source,ActiveConnection,CursorType,LockType,Options
Source
The Recordset object can be connected to the Command object through the Source property. The Source parameter can be a Command object name, a SQL command, a specified data table name or a Stored Procedure. If this parameter is omitted, the system uses the Source property of the Recordset object.
ActiveConnection
Recordset objects can connect to Connection objects through the ActiveConnection property. The ActiveConnection here can be a Connection object or a string of string parameters containing database connection information (ConnectionString).
CursorType
The CursorType parameter of the Open method of the Recordset object indicates what type of cursor the data will be started with, including adOpenForwardOnly, adOpenKeyset, adOpenDynamic and adOpenStatic, as described below:
-------------------------------------------------- ----------
Constant value description
-------------------------------------------------- ----------
adOpenForwardOnly 0 Default value, starts a cursor that can only move forward (Forward Only).
adOpenKeyset 1 starts a Keyset type cursor.
adOpenDynamic 2 starts a Dynamic type cursor.
adOpenStatic 3 starts a Static type cursor.
-------------------------------------------------- ----------
The above cursor types will directly affect all properties and methods of the Recordset object. The following list illustrates the differences between them.
-------------------------------------------------- ----------
Recordset properties adOpenForwardOnly adOpenKeyset adOpenDynamic adOpenStatic
-------------------------------------------------- ----------
AbsolutePage does not support reading and writing.
AbsolutePosition is not supported. Not supported. Readable and writable. Readable and writable.
ActiveConnection read-write read-write read-write read-write
BOF read only read only read only read only
Bookmark is not supported. It is not supported. It can be read and written. It can be read and written.
CacheSize Readable and writable Readable and writable Readable and writable Readable and writable
CursorLocation Readable and writable Readable and writable Readable and writable Readable and writable
CursorType Read and write Read and write Read and write Read and write
EditMode read-only read-only read-only read-only
EOF read only read only read only read only
Filter Readable and writable Readable and writable Readable and writable Readable and writable
LockType Read and write Read and write Read and write Read and write
MarshalOptions Readable and writable Readable and writable Readable and writable Readable and writable
MaxRecords Readable and writable Readable and writable Readable and writable Readable and writable
PageCount does not support read-only read-only
PageSize Readable and writable Readable and writable Readable and writable Readable and writable
RecordCount does not support read-only read-only
Source Readable and writable Readable and writable Readable and writable Readable and writable
State read only read only read only read only
Status read only read only read only read only
AddNew Support Support Support Support
CancelBatch support support support support
CancelUpdate support support support support
Clone is not supported. Not supported.
Close support support support support
Delete support support support support
GetRows support support support support
Move is not supported, supported, supported.
MoveFirst support support support support
MoveLast not supported supported supported supported
MoveNext support support support support
MovePrevious Not supportedSupportedSupported
NextRecordset support support support support
Open support support support support
Requery support support support support
Resync Not supported Not supported Supported Supported
Supports
Update Support Support Support Support
UpdateBatch support support support support
-------------------------------------------------- ----------
The NextRecordset method does not apply to Microsoft Access databases.
LockType
The LockType parameter of the Open method of the Recordset object indicates the Lock type to be used. If this parameter is ignored, the system will use the LockType property of the Recordset object as the default value. LockType parameters include adLockReadOnly, adLockPrssimistic, adLockOptimistic and adLockBatchOptimistic, etc., which are described as follows:
-------------------------------------------------- ----------
Constant constant value description
-------------------------------------------------- ----------
adLockReadOnly 1 Default value, Recordset object starts in read-only mode, cannot run AddNew, Update, Delete and other methods
adLockPrssimistic 2 When the data source is being updated, the system will temporarily lock other users' actions to maintain data consistency.
adLockOptimistic 3 When the data source is being updated, the system will not lock other users' actions, and other users can add, delete, and modify data.
adLockBatchOptimistic 4 When the data source is being updated, other users must change the CursorLocation attribute to adUdeClientBatch to add, delete, or modify data.