Highlights of common ASP script programs
1. asp and Access database connection:
<%
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
%>
2. ASP and SQL database connection:
<%
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
3. 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 data table where condition expression"
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 ...) Values (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") to obtain the calculation value. Use the same method for other functions.
(5) Create and delete data tables:
Create Table data table name (Field 1 type 1 (length), Field 2 type 2 (length )...... )
20 useful examples in ASP Programming
Author: Unknown
20 useful examples in ASP Programming
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
5. How to return to the previous page
A: <a href = "<% = request. servervariables (" http_referer ") %>"> preivous page </a>
Or use an image such as
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: Because ASP is run on the server, if you can display a dialog box on the server, you have to wait for someone to confirm before your program can continue to run, in general, the server won't be guarded, so Microsoft has to disable this function and tell you (:) haha) that it has 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 encrypt ASP scripts and client javas scripts/vbs scripts... However, after the client is encrypted, only ie5 can be executed. After the server script is encrypted, only s runtime engine 5 (installed with ie5) can be executed.
12. How can I transmit a query string from one ASP file to another?
A: response. Redirect ("second. asp? "& Request. servervariables (" QUERY_STRING "))
13. Does the global. Asa file always fail?
A: only when the web directory is set to Web application and global. ASA is valid, and global. ASA is valid under the root directory of a Web application. IIS4 can use Internet Service Manager to set application setting. How can I 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-> select 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 all. 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 DLL. This method is used from IIS 3.0 to IIS 4.0 and other Web servers. It needs to be executed in the command line mode, enter the directory containing DLL, and enter: regsvr32 component_name.dll such as c: \ temp \ regsvr32 aspemail. DLL, which 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: using MTS (Microsoft Transaction Server) MTS is a new feature of IIS 4, but it provides great 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 login ).
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 data table where condition expression"
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") to obtain the calculation value. Use the same method for other functions.
(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 the nth row 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 indicates whether the 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
The recordset 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 starts a keyset type cursor.
Adopendynamic 2 starts a dynamic type cursor.
Adopenstatic 3 starts 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
Bookmark does not support 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: readable/writable
Maxrecords
Pagecount does not support read-only and read-only
Pagesize: readable/writable
Recordcount does not support read-only and read-only
Source: readable/writable
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 is not supported.
Supported by movefirst
Supported by movelast
Supported by movenext
Supported by moveprevious
Supported by nextrecordset
Open Support
Requery support
Not supported by Resync
Supports support
Update support
Supported by updatebatch
--------------------------------------------------------------
The nextrecordset method is not applicable to Microsoft Access databases.
Locktype
The locktype parameter of the open method of the recordset object 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, which 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.