Common code for Web programming development

Source: Internet
Author: User
Tags execution expression insert sql first row access database how to use sql access
Web| programming
1. asp connect with Access database:
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
Conn.Open "Provider = Microsoft.jet.oledb.4.0;data Source =" & 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; "



Set Recordset object: Set Rs=server.createobject ("Adodb.recordset")
Rs.Open SQL statement, conn,3,2



How to use SQL Common commands:

Data record filtering: sql= "SELECT * from data table where field name = field value order By field name"
Sql= "SELECT * from data table where field name like '% field value ' Order By field name '
Sql= "SELECT top * from data table where field name order By field name"
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" 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 Condition expression "

Delete data record: sql= "delete from data table where condition expression"
Sql= "Delete from data table" (delete all records from datasheet)

Add data record: Sql= INSERT INTO datasheet (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 data table to the destination datasheet) data logging statistics 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

Methods referencing 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) ...)
Example: CREATE TABLE tab01 (name varchar, datetime default Now ())
drop table datasheet name (permanently deletes a data table)


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
To determine the data is a digital type
If not IsNumeric (Request (field name)) Then
Response.Write "Not Numbers"
Else
Response.Write "Number"
End If page execution time:


<%startime = Timer ()%>
.... ....
Content
... ...
End
<%
Dim Endtime
Endtime = Timer ()
Response.Write "Page Execution time: <font color=red>" &formatnumber ((endtime-startime) *1000,5) & "</font> MS"
%> define the size of the starting window when you open a Web page
<script for= "window" event= "onload" >
Window.resizeto (500,300)
</script>

Random number:
<%randomize%>
<%= (int (rnd () *n) +1)%>

The record keywords that are obtained when querying data are displayed in red:
Replace (RS ("field x"), Searchname, "<font color= #FF0000 >" & searchname & "</font>")
Using ASP to check whether the visitor used the agent
<% if Request.ServerVariables ("Http_x_forwarded_for") <> "" Then
Response.Write "<font color= #FF0000 > You have passed the proxy server," & _
"True IP is" &request.servervariables ("Http_x_forwarded_for")
End If
%>
Determine the origin of the previous page
Request.ServerVariables ("Http_referer")
Javascript:document.referrer

Clear Cache, Reload page
<%response.expires = 0
Response.ExpiresAbsolute = Now ()-1
Response.AddHeader "Pragma", "No-cache"
Response.AddHeader "Cache-control", "private"
Response.CacheControl = "No-cache"
%>

Retrieving and deleting duplicate records in a database
Conn.execute ("Delete from table where ID isn't in" (select distinct from table))
File deletion function <%
' File deletion function
function DeleteFile (filename)



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.