20 very useful examples in ASP programming

xiaoxiao2021-03-06  87

1. How to use ASP to judge your website's virtual physical path

A: Using the mappath method

<% = Server.mappath ("/")%>

2. How do I know the browser used by the user?

A: Using 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 access to daily

A: 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 sincennound 10,1998

4. How to display a random image

<% DIM P, PPIC, DPIC

PPIC = 12

Randomize

P = int (PPIC * RND) 1)

DPIC = "Graphix / Randompics /" & P & ". GIF"

%>

display

5. How to return to the previous page

Answer: preivous page

Or with images such as: <% = request.servervariables (>>>>>>

6. How to determine the other party's IP address

Answer: <% = Request.serverVariables ("remote_addr)%>

7. How to link to a picture

Answer: <% @ Languages ​​= VBS CRIPT%>

<% response.expires = 0

StrimageName = "graphix / errors / errooriamge.gif"

Response.Redirect (StrimageName)

%>

8. Forced input password dialog

A: This sentence is put on the beginning of the page

<% response.status = "401 not authorized"

Response.end

%>

9. How to transfer variables from one page to another

A: Use hidden type to transfer variables

<% form method = "post" action = "mynextpage.asp">

<% for each item in request.form%>

<% script language = 'javas cripe>>

Alert ("<% = Yourvar%>")

11. Is there any way to protect your source code, don't see it?

A: You can download a Microsoft Windows S Cript Encoder, which can encrypt the ASP script and client JavaS CRIPT / VBS CRIPT script. . . However, after the client encrypts, only IE5 can execute, after the server-side script is encrypted, only the S Cript Engine 5 installed on the server can be executed.

12. How can I send Query String from an ASP file to another?

A: The former file adds the following sentence: response.redirect ("second.asp?" & Required.servervariables ("query_string"))

13. Global.asa file always does not work?

A: Only the web directory is set to Web Application, Global.asa is valid, and Global.asa is valid in the root directory of a web application. IIS4 can use Internet Service Manager to set Application Setting how to make the HTM file like an ASP file can execute script code?

14. How can I make the script code as the HTM file like an ASP file?

Answer: Internet sevices manager -> Select Default Web Site -> Right Mark -> Menu Properties -> Home -> Application Setting -> Click button "Configuration" -> App Mapping -> Click button "Add "-> EXECUTABLE BROWSE Selection / Winnt/System32/INetsrv/asp.dll Extension Enter HTM Method Exclusions Enter Put.delete All OK. But it is worth noting that this is also processed by ASP.DLL for HTM, and the efficiency will be reduced.

15. How to register components

A: There are two ways.

The first method: Hand-registered DLL This method is used from IIS 3.0 to IIS 4.0 and other web server. It requires you to execute in the command line mode, enter the directory containing the DLL, and enter: regsvr32 component_name.dll, for example C: / temp / regsvr32 aspemail.dll, will register DLL's specific information in the registry in the registry in the server . This component can then be used on the server, but this method has a defect. When the component is registered with this method, the component must have the corresponding setting NT anonymous account with permission to execute this DLL. In particular, some components need to read the registry, so the method of this registration component is only used in the case where there is no MTS on the server. Use: regsvr32 / u aspobject.dll Example C: / Temp / Regsvr32 The second method of using the MTS (Microsoft Transaction Server) MTS is IIS 4, but it provides a huge improvement. MTS allows you to specify that only privileged users can access components and greatly improve the security settings on the website server. The steps to register the components on the 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) Naming the package.

5) Specify the Administrator account or use "Interactive" (if the server is often used using administrator).

6) Now use Right-click "Components" after the package you just created. Select "New Ten Component".

7) Select "Install New Component".

8) Locate your .dll file and select Next to complete.

To delete this object, just select its icon, then select Delete.

Note: Pay special attention to the second method, it is used to debug the best way to write components, without having to restart the machine each time.

16. ASP and Access database connection:

<% @ 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

%>

17. ASP is connected to the SQL database:

<% @ language = vbs cript%>

<%

DIM Conn

Set conn = server.createObject ("adoDb.connection")

Con. PROVIDER = SQLOLEDB; DATA SOURCE = SQL server name or IP address; UID = SA; PWD = database password; Database = database name

%>

Establish record set objects:

SET RS = Server.createObject ("AdoDb.Recordset") RS.Open SQL statement, CONN, 3, 2

18. SQL common command usage:

(1) Data Record Screening:

SQL = "SELECT * FROM data table where field name = field value ORDER BY field name [DESC]"

SQL = "SELECT * FROM DATA WHERE Field Name Like '% Field Value' Order By Field Name [DESC]"

SQL = "SELECT TOP 10 * FROM DATA WHERE Field Name ORDER BY Field Name [DESC]"

SQL = "SELECT * FROM DATA WHERE Field Name in ('value 1', 'value 2', 'value 3')"

SQL = "SELECT * FROM DATA WHERE Field Name BetWeen Value 1 AND Value 2"

(2) Update data record:

SQL = "Update Datasheet 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 Conditions Expression"

(3) Delete data records:

SQL = "Delete from Data Sheet WHERE Condition Expression"

SQL = "delete from data table" (deleted all records of data tables)

(4) Add a data record:

SQL = "INSERT INTO Data Sheet (Field 1, Field 2, Field 3 ...) Valuess (value 1, value 2, value 3 ...)"

SQL = "INSERT INTO Target Data Table Select * From Source Data Sheet" (Add record of the source data table to the target data table)

(5) Data logging statistics:

AVG (field name) draws a table column average

Count (* | field name) Statistics on the number of data lines or the number of data line counts on a certain column

Max (field name) get the largest value of a table bar

MIN (field name) gets the smallest value of a table column

SUM (field name) adds the value of the data bar

Citing the above function:

SQL = "SELECT SUM (Field Name) AS alias from Datasheet Where Condition Expression

SET RS = conn.excute (SQL)

Use RS ("alias") to get the value, and other functions are used.

(5) Establishment and deletion of data sheets:

Create Table Data Sheet Name (Field 1 Type 1 (Length), Field 2 Type 2 (Length) ...)

Example: Create Table Tab01 (Name Varchar (50), DateTime Default now ())

DROP TABLE Datasheet Name (permanently delete a data sheet)

19. Recording the method of the object:

Rs.MoveNext moves the record pointer down from the current position down

Rs.MovePrevious moves the record pointer from the current location

Rs.MoveFirst moves the record pointer to the first line of the data table

Rs.Movelast moves the record pointer to the last line of the data table

Rs.absolutePosition = n Moves the record pointer to the data table N

Rs.absolutePage = N Move the record pointer to the first line of the nth page

rs.pagesize = n Sets each page as N records

Rs.PageCount Returns the number of pages based on PageSize

Rs.Recordcount Returns the total number of records

RS.BOF Returns whether the pointer exceeds the data sheet header, True Represents whether false is rs.eof Returns the record pointer over the end of the data table, True is indicated by the false.

Rs.delete deletes the current record, but the record pointer will not move downward

Rs.addnew Add record to the end of the data

Rs.Update Update Data Table Record

--------------------------------

20 Recordset object method

Open method

Recordset.open Source, ActiveConnection, Cursortype, LockType, Options

Source

The Recordset object can connect the Command object via 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

The Recordset object can connect the Connection object through the ActiveConnection property. The ActiveConnection here can be a connection object or a string parameter containing database configuration (ConnectionsTRING).

Cursortype

The CURSORTYPE parameter of the RECORDSET object Open method represents what kind of cursor type starts data, including AdopenForward, AdoPenkey, AdoPENDYNAMIC, and AdoPenStatic, and is described below:

--------------------------------

Constant constant value description

--------------------------------

AdopenForwardonly 0 defaults to start a prominend that can only move forward.

AdopenKeyset 1 Start a KEYSET type cursor.

AdoPendynamic 2 launches a dynamic type cursor.

AdoPenStatic 3 launches a cursor for a Static type.

----------------------------------------------------- List description differences between them.

--------------------------------

Recordset property adopenforwardonly adopenkeyset adoPENDYNAMIC AdoPenStatic

--------------------------------

AbsolutePage does not support not support readable write readable

AbsolutePosition does not support not support readable write readable

ActiveConnection readable write readable write readable write readable

Bof read-only Read read only read only

Bookmark does not support not support readable write readable

Cachesize readable write readable write readable write readable

CursorLocation Readable Writables Readable Writing Writing Writing

CURSORTYPE readable write readable write readable write readable

EditMode reads read only read only read only

EOF read-only Read read only read only

Filter readable write readable write readable write readable

LockType readable to read-readable write-readable

Marshaloptions readable and writable readable write readable

MaxRecords readable write readable write readable write readable

PageCount does not support only read-only read only

PageSize readable write readable write readable write readable

Recordcount does not support not support read only read only

Source Readable Writable Writable Writable Writing

State Read Read Read Read Read Read

Status is read-only Read read only read only

AddNew support support support support

Cancelbatch support support support support

CancelupDate support support support support

Clone does not support

Close Support Support Support

Delete support support support support

GETROWS support support support support

MOVE does not support support support support

MoveFirst support support support support

MovelaST does not support support support support

MoveNext support support support support

MovePrevious does not support support support support

NextRecordset support support support support

Open support support support support

Requory support support support support

RESYNC does not support support support Supply Supports Support Support Support

UPDATE support support support support

UpdateBatch support support support support

--------------------------------

Where the next NEXTRECORDSET method does not apply to the Microsoft Access database.

LockType

The LockType parameter of the RECORDSET object Open method indicates the Lock type to be used. If this parameter is ignored, the system will be preset with the LockType property of the Recordset object. The LockType parameter contains AdlockReadOnly, AdlockPrsSIMistic, AdlockOptimistic, and AdlockbatchOptimistic, etc., which are described below:

--------------------------------

Constant constant value description

--------------------------------

AdlockReadOnly 1 default, Recordset objects are started in read-only mode, unable to run AddNew, Update, and Delete.

AdlockPrssimistic 2 When the data source is being updated, the system is temporarily locked to maintain data consistency.

AdlockOptimistic 3 When the data source is being updated, the system does not lock the action of other users, and other users can increase, delete, and change the data.

AdlockBatchOptimistic 4 When the data source is being updated, other users must change the CURSORLOCATION property to ADUDECLIENTBATCH to increase, delete, and change the data.

转载请注明原文地址:https://www.9cbs.com/read-124622.html

New Post(0)