20 very useful examples in ASP programming

xiaoxiao2021-03-06  63

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

the physical path to this Virtual Website IS : <% = 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 average number of repetitive visits daily 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 has referenced number 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 >"> preivous page arrowback.gif "alt =" < % = Request.ServerVariables ("http_referer")%>>> How to determine the other party's IP address A: <% = Request.serverVariables ("remote_addr)%> 7. How to link to a pair of images: <% @ Languages ​​= VBS CRIPT%> <% response.expires = 0 strimagename = "graphix / errors / erroriamge.gif"

Response.Redirect (StrimageName)%> 8. Forced Enter Password dialog A: Put this sentence to the beginning of the page <% response.status = "401 Not Authorized" response.end%> 9. How to transfer variables from one page To another: Use hidden type to pass the variable <% form method = "post" action = "mynextpage.asp"> <% for each item in request.form%> "Type =" hidden "value =" <% = server.htmlencode (Request.form (item))%> "> <% next%> 10. Why use MsgBox in the ASP program, the program is wrong No authority answer: Since the ASP is the server running, if you can display a dialog in the server, then you have to wait after the determination, your program can continue to execute, and the general server will not be defended, so Microsoft has to Prohibit this function and tell you (:) Oh) no permissions. 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 be performed on the ASP script and client Java CRIPT / VBS CRIPT script. encryption. . . 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?") 13.Global.asa file always does not work? A: Only the web directory is set to Web Application, Global.asa is only 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 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: / 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 and SQL database connection: <% @ language = VBS cript%> <% DIM Conn Set Conn = Server.createObject ("AdoDb.Connection") Con -.open "provider = SQLOLDB; DATA SOURCE = SQL server name or IP address; UID = sa; PWD = database password; Database = Database Name%> Establish a record Object: SET RS = Server.createObject ("AdoDb.Recordset") RS.Open SQL statement, conn, 3,2 18. SQL Common command usage: (1) Data Record Filter: SQL = "SELECT * FROM Data Sheet 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 WHERE Field Name in ('value 1', 'value 2', 'value 3') "SQL =" SELECT * FROM Datasheet WHERE field BETWEEN Value 1 and Value 2 "(2) Update Data Record: SQL =" Update Datasheet Set Field Name = Field Value Where Condition Expression "SQL =" Update Datasheet 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 "(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 "(Add Source Data Sheet Add to Target Data) (5) Data Recording Statistics: AVG (field Name) Since a table column average count (* | field name) Statistics on the number of data lines or the number of data row statistics on a certain column value MAX (field name) get the largest value min (field name) ) A value of the smallest value of a table bar adds the value of the value of the data bar to the above function: SQL = "

Select Sum (Field Name) AS Alias ​​from Datasheet Where WHERE Condition Expression "SET RS = Conn.Excute (SQL) Using RS (" Alias ​​", the value is obtained, and the other functions apply the same. (5) Establishment of the data sheet And delete: CREATE TABLE Table 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 deleted a data table) 19. Method for record the set object: rs.Movenext Move the logger from the current location RS.MOVEPREVIOUS to move the record pointer from the current position up row RS.MoveFirst will record the pointer To the data table, the first line of RS.Movelast moves the record pointer to the data table last row rs.absolutePosition = N Move 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 for n-record RS.PageCount Returns the total number of pages according to PageSize Rs.Recordcount Returns the total number RS.BOF Returns the record pointer to be exceeded the data table header, True is indicated, whether false is rs. .EOF Returns whether the pointer exceeds 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 add the record to the data table end RS.UPDate Update Data Table Record - -------------------------------------- 20 RecordSet Objects Open Method Recordset.open Source, ActiveConnection , CURSORTYPE, LOCKTYPE, OPTIONS SOURCE RecordSet objects can connect the Command object via the Source property. Source parameter can be a Command object name, a SQL command, a specified data table name or a stiled procedure. If this parameter is omitted, the system is Source attributes with Recordset objects. ActiveConnection Reco The RDSET 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 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 absolutePosition does not support not support readable write readable ActiveConnection readable to read-readable write read-read read-only BOF read-only read-only read-only BOOKMARK does not support readable write readable Cachesize readable WrTable Write-readable Writing CACHESIZE CURSORLOCATION can be read from or write read read write CursorType readable, writable read-only and writable read-only read-only and writable read only EditMode EOF read-only read-only and writable read-only and writable read-only Filter writable Readable to write lockType readable write readable write readable write readable MarshalOptions readable write readable write readable write readable MaxRecords readable write readable write readable write readable write PageCount does not support 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-only AddNew support support support can support CancelBatch support support support CancelupDate support support support Clone does not support close 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 support support Open Support Support Support Requory Support Support Support RESYNC does not support support support support Supports support support support support UPDATE support support support support UPDATEBATCH Support support support ------------------------------------------------------------------------------------------------------------------ -------------------------------------------- The next NEXTRECORDSET method is not applicable. 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. The LockType parameter contains AdlockReadonly, AdlockPrsSIMistic, AdlockOptimistic, and AdlockBatchOptimistic, etc., is described below: ------------------------------------------------------------------------------------------------------------------------------------ -------------------------- constant constant value description ------------------- ------------------------------------------ AdlockReadOnly 1 default value, Recordset Objects are launched in read-only way, and how to operate AddNew, Update, and Delete. AdlockPrsSIMistic 2 when the data source is being updated, the system is temporarily locked to maintain data consistency.

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

New Post(0)