ASP classic code example

xiaoxiao2021-03-06  119

1. How to use ASP to judge your website's virtual physical path: Using mappath method

The physical path to this Virtual Website IS: < / font> <% = server.mappath ()%> 2. How do I know the browser used by the user? A: a method using the Request object 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 daily Average Average Repeated Visit: 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 has referenced Sincen November 10,1998 4. How to display random image <% DIM P, PPIC, DPIC PPIC = 12 Randomize P = Int (PPIC * RND) 1) DPIC = graphix / randompics / & p & .gif%> Show > 5. How to return to previous page answers: > preivous page or with images such as: <%>>> How to determine the other party's IP address answer: <% = request .servervariables (transote_addr)%> 7. How to link to a pair of pictures: <% @ Languages ​​= VBS CRIPT%> <% response.expires = 0 strimagename = graphix / errors / erroriamge.gif response.redirect (StrimageName) %> 8. Forced Enter Password dialog: Put this sentence to the beginning of the page <% response.status = 401 Not au Thorized response.end%> 9. How to transfer variables from one page to another: Use hidden type to transfer variable <% form method = post action = mynextpage.asp> <% for Each Item in Request.form%>>

type = hidden value = <% = Server.htmlencode (Request.form (item))%>> <% next%> 10. Why do I use within an ASP program? Msgbox, the program error said no right to answer: Since the ASP is running, if you can display a dialog box, then you have to wait until you have pressed, your program will continue to execute, and the general server will not be defeated. So Microsoft has to ban this function and tell you (:) Oh) no permission. However, ASP and client script combination can display a dialog box, as Follows: <% Yourvar = test dialog,%> <% s cript language = 'javas cript'> alert (<% = Yourvar%>) 11. Is there a way to protect your source code, do not give people an answer: You can download a Microsoft's 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? & Request.servervariables (query_string)) 13.Global.asa file always does not work? A: Only the web directory is set to Web Application, Global.asa Effective, and the root directory of a Web Application is valid. 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 Select WinntSystem32inetsRvasp.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 the component 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: EMP EGSVR32 Aspemail.dll, it will register the DLL's specific information in the registry in the registry.

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: EMP EGSVR32 / U Aneiodbc.dll The second method: Using the MTS (Microsoft Transaction Server) MTS is IIS 4 new features, 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 logged in using administrator). 6) Now use the right-click on the package you just created under the company. 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.cronection) conn.open driver = {Microsoft Access Driver (* .mdb)}; UID = Admin; PWD = Database Password; DBQ = & MDBFile%> 17. ASP and SQL Database Connection: <% @ 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%> Establish record set object: set = server.createObject (AdoDb.Recordset) RS.Open SQL statement, conn, 3, 2 18. SQL common command usage: (1) Data record filter: SQL = SELECT * FROM Datasheet 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 Datasheet 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 WHERE Field Name BetWeen Value 1 AND Value 2 (2) Update Data Record: 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 Record: SQL = Delete from Data Sheet WHERE Condition Expression SQL = Delete From Datasheet (Delete Data Table Remove) (4) Add 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 (Table Source Data Sheet " Add to Target Data Sheet) (5) Data Recording Statistics: AVG (Field Name) Licening a table bar average value count (* │ field name) Statistics on the number of data or values ​​for a certain column Row Statistics Max (Field Name) gets the maximum value of a table column min (field name) gets the smallest value SUM (field name) with the smallest value of a table bar, adds the value of the value of the data bar. Method: SQL = SELECT SUM Field Name) AS Alias ​​from Datasheet Where WHERE Condition Expression SET RS = Conn.excute (SQL) Gets the values ​​for the RS (alias), and other functions are used.

(5) Establishment and deletion of data sheet: CREATE TABLE DATA Name (Field 1 Type 1 (Length), Field 2 Type 2 (Length) ...) Example: Create Table Tab01 (Name Varchar (50), DateTime Default NOW )) DROP TABLE Dataset Name (Permanent Delete A Data Table) 19. Method of Recording Set Objects: rs.Movenext Moves the Record Pointer from the current location RS.MOVEPREVIOUS to move the record pointer up from the current location Rs.Movefirst Move the record pointer to the data table RE. RS.MOVELAST Move the record pointer to the last line RS.AbsolutePosition = N Move the record pointer to the data table Nth line rs.absolutepage = N Move the record pointer to move the record pointer The first line of the nth page RS.PageSize = N Sets each page for n-record RS.PageCount Returns the total number of pages based on PageSize Rs.Recordcount Returns the total number RS.BOF Returns the record pointer over the data sheet header, TRUE Indicates whether false is rs.eo returns the record pointer over the end of the data table, the true representation is whether false deletes the current record, but the record pointer does not move the RS.AddNew to the data table end RS .Update update data table record -------------------------------------- 20 RecordSet object method Open Method RecordSet.open Source, ActiveConnection, Cursortype, LockType, Options Source RecordSet objects 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 RecordSet objects 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 RECORDSET Object Open method The CURSORTYPE parameter represents what kind of cursor type starts data, including AdopenForwardOnly, AdoPENDYNAMIC, and AdoPenStatic, is described below: ---------------------------------------------------------------------------------------------------------------------- ------------------------------------------- constant constant value description - -------------------------------------------------- --------- AdopenForwardonly 0 default, start a cacope 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. -------------------------------------------------- ----------- The above cursor type will directly affect all the properties and methods all of the Recordset object, the following list describes the differences between them. -------------------------------------------------- ----------- RECORDSET Property AdopenForwardonly AdopenKeyset AdoPENDYNAMIC ADOPENSTATIC --------------------------------- ---------------------------- AbsolutePage does not support you do not support readable write readable write absolutePosition does not support readable write ActiveConnection readable and writable readable write readable write-readable and write-only bookmark does not support you read / write Cachesize readable and write Cachesize Ready-write read-read-read-read-read-read-read-read-read-read-read-read-only read-read-read-read-read-read-read-read-read-read-read-only-read-read-only-read-read-read-like read-read-only-read-read-only-read-read-only-read-read-only-read-read-only-read-read-only-minded write LockType can read and write can read and write can read and write can read and write can read and write MarshalOptions can read and write can read and write can read and write can read and write MaxRecords can read and write can read and write can read and write can read and write PageCount only not supported not supported read-only read PageSize can read and write can read and write can read and write can read and write RecordCount not supported not supported read only read-only Source can read and write can read and write can read and write can read and write State read-only read-only read-only read-only read-only read-only Status Read-only AddNew support support support support CancelBatch support support support CancelupDate support support support CLONE does not support unsupported support support support support support support support support support support GETROWS support support support MOVE does not support support support MOVEFIRST support support support MoveLAST does not support support support MOVENEXT support support support MOVEPREVIOUS does not support support support NEXTRECORDSET Support Support Open Support Support Support REQUERY Support Support Support Supporting RESYNC does not support support support support support Support support support support UPDATE support support support support UPDATEBATCH Support support support -------------------------- ---------------------------------- in which the nextRecordset method does not apply to the Microsoft Access database. The LockType parameter of the LockType 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.


New Post(0)