20 useful examples in ASP Programming
1. How to use ASP to determine the virtual physical path of your website
A: 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 used by the user?
A: 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 repeated visits per day
Answer: Solution
<% Startdate = datediff ("D", now, "01/01/1990 ")
If
Strdate <0 then startdate = startdate *-1
Avgvpd = int (usercnt)/startdate) %
>
Display result
<% Response. Write (avgvpd) %>
That is it. this page
Have 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"
%>
Display
Src = "<% = dpic %>">
5. How to return to the previous page
A: <a href = "<
% = Request. servervariables ("http_referer") %> "> preivous page </a>
Or use an image, for example, % = Request. servervariables ("http_referer") %> ">
6. How to Determine the IP address of the other party
Answer: <% = request. servervariables ("remote_addr) %>
7. How to link to an image
A: <% @ ages = vbs versions %>
<% Response. expires = 0
Strimagename = "Graphix/errors/erroriamge.gif"
Response. Redirect (strimagename)
%>
8. Force password input dialog box
A: Put this sentence at the beginning of the page.
& Lt; % response. Status = "401 not authorized"
Response. End
%>
9. How to pass variables from one page to another
A: Use the hidden type to pass variables.
<% Form method = "Post"
Action = "mynextpage. asp">
<% For each item in request. Form %>
<Input namee = "<% = item %>" type = "hidden"
Value = "<
% = Server. htmlencode (request. Form (item) %> ">
<% Next %>
</Form>
10. Why do I use msgbox in an ASP program? If a program error occurs, I have no permission.
A: As ASP
It is run on the server. If a dialog box can be displayed on the server, you have to wait for someone to confirm before your program can continue to be executed. Generally, no one will guard the server, so Microsoft has to disable this function and tell you at will.
(:) Haha) no permission. However, the combination of ASP and client scripts can display a dialog box, as follows:
<% Yourvar = "test dialog box" %
>
<% S required language = 'javas upload'>
Alert ("<% = yourvar %
> ")
</S response>
11. Is there any way to protect your source code?
A: You can download a Microsoft Windows
S cript encoder, which can be used for ASP scripts and client javas logs/vbs
... However, after the client is encrypted, only ie5 can be executed. After the server script is encrypted, only the server is installed with the S Engine
5 (when an Internet Explorer 5 is installed, it can be executed.
12. How can I transmit a query string from one ASP file to another?
A: Add the following statement to the former file:
Response. Redirect ("second. asp? "& Request. servervariables (" QUERY_STRING "))
13. Does the global. Asa file always fail?
A: Only the web directory is set to Web application,
Global. ASA is valid, and global. ASA is valid under the root directory of a Web application. IIS4 can use Internet Service
How does manager set application setting to make the HTM file execute script code like an ASP file?
14. How can I make the HTM file execute script code like an ASP file?
Answer: Internet Sevices Manager->
Choose Default web site-> right mouse key-> menu properties-> Home Directory-> application setting )-
> Click "Configure"-> app mapping-> click "add"-> executable browse and select
/Winnt/system32/inetsrv/asp. dll extension input HTM method exclusions input put. Delete
OK. However, it is worth noting that the HTM should also be processed by ASP. dll, reducing the efficiency.
15. How to register Components
A: There are two methods.
Method 1: manually register the DLL. This method is used from IIS 3.0 to IIS 4.0 and other Web
Server. It needs to be executed in the command line mode, enter the directory containing DLL, and enter: regsvr32 component_name.dll for Example
C:/temp/regsvr32 aspemail. dll
It registers the specific DLL information into the registry on the server. Then this component can be used on the server, but this method has a defect. After the component is registered using this method, the component must have the permission to execute the DLL by setting the NT anonymous account. In particular, some components need to read the Registry. Therefore, the method of registering this component is to cancel the registration of this DLL only when there is no MTS on the server, and use: regsvr32
/U aspobject. DLL example C:/temp/regsvr32/u aneiodbc. dll
Method 2: Use MTS (Microsoft Transaction Server)
4. New features, but they provide significant improvements. MTS allows you to specify that only privileged users can access components, greatly improving the security settings on the website server. To register a component on MTS, follow these steps:
1) Open the IIS 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 often uses administrator to log on ).
6)
Right-click the expanded "components" under the package you just created ". Select "New then component ".
7) Select "Install
New component ".
8) Find Your. dll file and select next.
To delete this object, select its icon and then delete.
Note: pay special attention to the second method, which is the best way to debug your own component, instead of restarting the machine every time.
16. asp and Access database connection:
<% @ Language = vbs certificate %>
<%
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 = vbs certificate %>
<%
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 record set object:
Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open SQL statement, Conn, 3, 2
18. Common SQL commands:
(1) Data Record Filtering:
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 records:
SQL = "update data table set field name = field value where condition expression"
SQL = "update data table
Set field 1 = value 1, Field 2 = value 2 ...... Field n = value n where condition expression"
(3) Delete data records:
SQL = "delete from
Where condition expression of a data table"
SQL = "delete from data table" (delete all data table Records)
(4) add data records:
SQL = "insert into data table (Field 1, Field 2, Field 3 ...) Valuess (value 1, value 2, value 3 ...) "
SQL = "insert
Into target data table select * from source data table "(add records of source data table to target data table)
(5) statistical functions of data records:
AVG (field name)
Returns the average value of a table column.
Count (* | field name) statistics on the number of data rows or the number of data rows with values in a column
Max (field name) obtains the maximum value of a table column.
Min (field name) obtains the minimum value of a table column.
Sum (field name) adds the values in the data column
The method for referencing the above functions:
SQL = "select
Sum (field name) as Alias from data table where condition expression"
Set rs = conn. excute (SQL)
Use RS ("alias ")
Returns the value of a system. Other functions are used in the same way.
(5) Create and delete 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. Method of record set object:
Rs. movenext
Moves the record pointer down a row from the current position
Rs. moveprevious transfers the record pointer from the current position to a row up
Rs. movefirst
Move 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
Move the record pointer to row N of the data table
Rs. absolutepage = n move the record pointer to the first row of page n
Rs. pagesize = N set N records per page
Rs. pagecount the total number of pages returned Based on pagesize settings
Rs. recordcount total number of returned records
Rs. bof
Whether the returned record pointer exceeds the first end of the data table. True indicates yes, and false indicates no.
Rs. EOF indicates whether the returned record pointer exceeds the end Of the data table. True indicates yes, and false indicates no.
Rs. Delete deletes the current record, but the record pointer does not move down
Rs. addnew add record to end of data table
Rs. Update update data table records
----------------- ------------------ ----
20 recordset object Method
Open Method
Recordset. Open
Source, activeconnection, cursortype, locktype, options
Source
Recordset
The object can be connected to the command object through the source attribute. The source parameter can be a command object name, an SQL command, a specified data table name, or a stored
Procedure. If this parameter is omitted, the system uses the source attribute of the recordset object.
Activeconnection
The recordset object can be connected to the connection object through the activeconnection attribute. Activeconnection can be a connection object or a string containing the database connection information (connectionstring.
Cursortype
The cursortype parameter of the open method of the recordset object indicates the type of cursor to start data, including adopenforwardonly, adopenkeyset, adopendynamic, and adopenstatic, which are described as follows:
--------------------------------------------------------------
Constant Value description
-------------------------------------------------------------
The default value of adopenforwardonly 0 is to start a forward-only cursor (forward only ).
Adopenkeyset 1
Start a keyset cursor.
Adopendynamic 2 starts a dynamic type cursor.
Adopenstatic 3
Start a static cursor.
-------------------------------------------------------------
The preceding cursor types directly affect all attributes and methods of the recordset object. The following lists the differences between them.
-------------------------------------------------------------
Recordset attribute adopenforwardonly adopenkeyset adopendynamic adopenstatic
-------------------------------------------------------------
Absolutepage does not support read/write.
Absoluteposition does not support read/write.
Activeconnection can be read/written or read/write
Bof read-only
Not supported by bookmark
Read/write
Cachesize: readable/writable
Cursorlocation: readable/writable
Cursortype can be read/written or read/write
Editmode read-only
EOF read-only
The filter can read and write, and can read and write.
Locktype: readable/writable
Marshaloptions read/write
Read/write:
Maxrecords
Pagecount does not support read-only and read-only
Pagesize: readable/writable
Recordcount does not support read-only and read-only
Source can be read/written
Read/write
State read-only
Status read-only
Addnew support
Support for cancelbatch
Supported cancelupdate
Clone is not supported.
Support for close
Support for Delete
Getrows support
Move
Not Supported
Supported by movefirst
Supported by movelast
Movenext support
Supported
Supported by moveprevious
Supported by nextrecordset
Open Support
Supported
Requery support
Not supported by Resync
Support for supports
Supported
Update support
Supported by updatebatch
--------------------------------------------------------------
The nextrecordset method is not applicable to Microsoft Access databases.
Locktype
Recordset
The locktype parameter of the object open method indicates the lock type to be used. If this parameter is ignored, the system uses the locktype attribute of the recordset object as the default value. The locktype parameters include adlockreadonly, adlockprssimistic, adlockoptimistic, and
Adlockbatchoptimistic and so on, are described as follows:
-------------------------------------------------------------
Constant Value description
--------------------------------------------------------------
Adlockreadonly 1 is the default value. The recordset object is started in read-only mode and cannot run addnew, update, delete, and other methods.
Adlockprssimistic 2 when the data source is being updated, the system will temporarily lock the actions of other users to maintain data consistency.
Adlockoptimistic
3. When the data source is being updated, the system does not lock the actions of other users. 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 and modify operations.