20 very useful examples in ASP programming (1)
1. How to use ASP to determine the virtual physical path of your website
Answer: Use the Mappath method: < %= Server.MapPath(/)% >
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 won't be guarded, so Microsoft has to disable this function and randomly tell you (haha) that you don't have permission. However, the combination of ASP and client script can display a dialog box, as follows:
<%yourVar=Test Dialog%>
Light rain is here
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/SYSTEM32/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 in command line mode, enter the directory containing the DLL, and enter: regsvr32 component_name.dll. For example, c:/temp/regsvr32 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/regsvr32 /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.
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:
<%@ language=VBscript%>
<%
dim conn,mdbfile
mdbfile=server.mappath(database name.mdb)
set conn=server.createobject(adodb.connection)
conn.open driver={microsoft access driver (*.mdb)};uid=admin;pwd=database password;dbq=&mdbfile
%>
17. ASP and SQL database connection:
<%@ language=VBscript%>
<%
dim conn
set conn=server.createobject(ADODB.connection)
con.open PROVIDER=SQLOLEDB;DATA SOURCE=SQL server name or IP address;UID=sa;PWD=database password;DATABASE=database name
%>
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
Recordset objects can be connected to Command objects 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.