Application Techniques of ASP Programming 20 cases

Source: Internet
Author: User
Tags servervariables first row how to use sql

1. How to use ASP to determine the virtual physical path of your website
Answer: Using 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
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 repeated visits per day
Answer: the solution
% Startdate=datediff (d,now,01011990)
If strdate 0 then startdate=startdate-1
Avgvpd=int ((usercnt) startdate)%
Show results
% Response.Write (AVGVPD)%
This 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 ((ppicrnd) +1)
Dpic=graphixrandompics&p&.gif
%
Show
IMG src=%=dpic%
5. How to get back to the previous page
Answer: A href=%=request.servervariables (http_referer)% preivous page A
Or with pictures such as:
IMG src=arrowback.gif alt=%=request.servervariables (http_referer)%
6. How to determine the IP address of each other
Answer:%=request.servervariables (REMOTE_ADDR)%
7. How to link to a picture
Answer:% @Languages =vbs cript%
% response.expires=0
Strimagename=graphixerrorserroriamge.gif
Response.Redirect (Strimagename)
%
8. Forced Enter Password dialog box
A: Put this sentence on the beginning of the page
% 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 per item in Request.Form%
Input namee=%=item% Type=hidden
Value=%=server. HTMLEncode (Request.Form (item))%
% Next%
Form
10. Why I use MsgBox in the ASP program, the program error said no permissions
A: Because the ASP is the server running, if you can display a dialog box on the server, then you have to wait for someone to confirm, your program can continue to execute, and the general server will not be guarded, so Microsoft has to prohibit this function, and told you do not have permissions. However, ASP and client-side scripting can be combined to display a dialog box, as follows
% yourvar= Test dialog box%
% s cript language= ' Javas cript '
Alert (%=yourvar%)
S Cript
11. There is no way to protect their source code, do not give people to see
A: You can download a Microsoft Windows S Cript encoder that encrypts ASP scripts and client Javas criptvbs cript scripts.
12. How can I transfer query string from one ASP file to another
A: The former file adds the following sentence:
Response.Redirect (second.asp & Request.ServerVariables (query_string))
13.global.asa files always don't work
A: Only the web directory is set to Web application, Global.asa is valid, and global.asa the root directory of a Web application is valid. IIS can use Internet Service Manager Settings Application setting how can I make an HTM file Execute script code like an ASP file?
14. How to make the HTM file can execute script code like ASP file
A: Internet sevices Manager-Select Default Web site-right mouse button-Menu Properties-〉 Home Directory-Application settings (Application Setting)-click the button configuration-app mapping- Click the button add-executable browse to select Winntsystem32inetsrvasp. DLL EXTENSION input HTM method exclusions input put.delete all OK. However, it should be noted that the HTM is also handled by Asp.dll, and the efficiency will be reduced.
15. How to register components
Answer: There are two ways.
The first method: Manually registering DLLs this way IIS always uses and other Web servers. It requires you to execute it on the command-line, into the directory containing the DLL, and enter: regsvr32 component_name.dll For example Ctempregsvr32 AspEmail.dll It registers specific information about the DLL into the registry on the server. This component can then be used on the server, but there is a flaw in this approach. When the component is registered with this method, the component must be set to the appropriate NT anonymous account to execute the DLL. In particular, some components need to read the registry, so the method of registering the component is simply using the server without MTS, to unregister the DLL, using: regsvr32 u aspobject.dll Example ctempregsvr32 u Aneiodbc.dll
The second approach: using MTS (Microsoft Transaction Server) MTS allows you to specify that only privileged users have access to the component, greatly improving the security settings on the Web server. The steps for registering 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 with the administrator).
6 now use the right mouse button to click the components that you have just built underneath the package. Select New then Component.
7 Select Install new component [B].
8 Locate your. dll file and select next to complete.
To delete this object, simply select its icon, and then select Delete.
Note: In particular, note the second method, which is the best way to debug your own writing components, without having to reboot the machine every time.

ASP to connect to an Access database:
<%@ Language=vbs cript%>
<%
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
%>
ASP to connect with SQL database:
<%@ Language=vbs cript%>
<%
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
%>
To set up a Recordset object:
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open SQL statement, conn,3,2
How to use SQL frequently used 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 * 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 data table where condition expression"
Sql= "Delete from data table" (delete all records from datasheet)
(4) Adding data records:
sql= INSERT into Data table (field 1, Field 2, Field 3 ...) valuess (value 1, value 2, value 3 ...)
Sql= INSERT INTO Target datasheet SELECT * from source datasheet (add a record from the source datasheet to the destination datasheet)
(5) Data record statistic function:
AVG (field name) to derive a table column average
Count (*| field name) statistics on the number of data rows or the number of data rows that have a value for a column
Max (field name) gets the maximum value of a table column
Min (field name) gets the smallest value of a table column
sum (field name) adds the value of the data bar
How to reference the above function:
Sql= "Select sum (field name) as Alias from data table where condition expression"
Set Rs=conn.excute (SQL)
Using RS ("Alias") to obtain the value of the unified, other functions using the same.
Data table creation and deletion:
CREATE Table datasheet Name (field 1 type 1 (length), Field 2 type 2 (length) ...)
Cases:
CREATE TABLE TAB01 (name varchar, datetime default Now ())
DROP Table Data table name
(Permanently deletes a data table)
19. Method of Recordset object:
Rs.movenext moves the record pointer down one row from the current position
Rs.moveprevious move the record pointer up one row from the current position
Rs.movefirst move the record pointer to the first row of the datasheet
Rs.movelast move the record pointer to the last row of the datasheet
Rs.absoluteposition=n move the record pointer to the nth row of the datasheet
Rs.absolutepage=n move the record pointer to the first row of page N
Rs.pagesize=n set every page to N records
Rs.pagecount returns the total number of pages based on pagesize settings
Rs.recordcount returns the total number of records
RS.BOF returns whether the record pointer is over the first end of the datasheet, True indicates Yes, FALSE is no
Rs.eof returns whether the record pointer is over the end of the datasheet, True indicates Yes, FALSE is no
Rs.delete deletes the current record, but the record pointer does not move down
Rs.addnew add records to the end of the datasheet
Rs.update Update datasheet Record
Recordset Object method
Open method
Recordset. Open source,activeconnection,cursortype,locktype,options
Source
The Recordset object can connect to the command object through the Source property. The source parameter can be a command object name, a section of SQL command, a specified data table name, or a stored Procedure. If this argument is omitted, the system takes the source property of the Recordset object.
ActiveConnection
The Recordset object can connect connection objects by ActiveConnection properties. The ActiveConnection can be a connection object or a string parameter containing the database connection information (ConnectionString).
CursorType
The CursorType parameter of the Recordset object Open method indicates what cursor type will start the data, including adOpenForwardOnly, adOpenKeyset, adOpenDynamic, and adOpenStatic. It is described as follows:
Constants Constant Numerical description
adOpenForwardOnly 0 defaults to start a cursor that can only be moved forward (Forward only)
adOpenKeyset 1 launches a keyset type of cursor
adOpenDynamic 2 launches a dynamic-type cursor
adOpenStatic 3 launches a static type of cursor

These cursor types will directly affect all properties and methods of the Recordset object, and the following list illustrates the differences between them.
Recordset Properties
Where the NextRecordset method does not apply to Microsoft Access databases.
LockType
The LockType parameter of the Recordset object's Open method represents the lock type to take, and if this argument is omitted, then the system takes the LockType property of the Recordset object as the preset value. The LockType parameters include adLockReadOnly, adlockprssimistic, adLockOptimistic, and adLockBatchOptimistic, as described below:
Constants Constant Numerical description
adLockReadOnly 1 defaults, the Recordset object starts as read-only and cannot run AddNew, Update, and Delete methods
Adlockprssimistic 2 When the data source is being updated, the system temporarily locks 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, change the operation of the data
adLockBatchOptimistic 4 When the data source is being updated, other users must change the CursorLocation property to Adudeclientbatch to add, delete, and modify the data

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.