Essence article: SQL statement Reference and Recordset object in ASP

Source: Internet
Author: User
Tags constant expression functions insert connect table name first row unsupported
sql| Reference | object | recordset | essence | statement 1. asp Connect with Access database:
  
<%@ language=vbscript%>
<%
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:
  
<%@ language=vbscript%>
<%
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
%>
  
To set up a Recordset object:
  
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open SQL statement, conn,3,2
  
   3. SQL Common Command use method:
  
(1) 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 (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 records from datasheet)
  
(4) Adding data records:
  
sql= INSERT into Data table (field 1, Field 2, Field 3 ...) VALUES (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)

(5) Data logging statistics function:

AVG (field name) draw a table column flat Average
COUNT (*| field name) statistics on the number of data rows or the number of data rows that have values for a column
MAX (field name) get the maximum value of a table column
min (field name) get a table column minimum value
sum (field name) to add the value of the data bar

Methods that reference the above functions:

sql= "Select sum (field name) as Alias from data table where condition expression"
set Rs=conn.excute (SQL)
br> uses RS ("Alias") to obtain the value of the EC, and other functions are used as above.

(5) 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 delete a data table)
  
4. Method of Recordset object:

Rs.movenext Move the record pointer down one line from the current position
rs.moveprevious move the record pointer up one line from the current position
Rs.movefirst moves the record pointer to the first row of the datasheet
Rs.movelast moves the record pointer to the last row of the datasheet
Rs.absoluteposition=n moves the record pointer to the nth row of the datasheet
Rs.absolutepag E=n moves the record pointer to the first row of page N,
Rs.pagesize=n sets each page to n records
Rs.pagecount returns the total number of pages based on pagesize settings
Rs.recordcount return total 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
  
-------------------
  
Recordset Object Methods
  
Open method

Recordset. Open source,activeconnection,cursortype,locktype,options
  
Source

The Recordset object can connect to the command object through the Source property. The source parameter can be a command object name, a section of SQL command, a specified data table name, or a stored Procedure. If this argument is omitted, the system takes the source property of the Recordset object.
  
ActiveConnection

The Recordset object can connect connection objects by ActiveConnection properties. The ActiveConnection can be a connection object or a string parameter containing the database connection information (ConnectionString).

CursorType

The CursorType parameter of the Recordset object Open method indicates what cursor type will start the data, including adOpenForwardOnly, adOpenKeyset, adOpenDynamic, and adOpenStatic. It is described as follows:

---------------------------------
Constants Constant Numerical description
----------------------------------
adOpenForwardOnly 0 defaults to start a cursor that can only be moved forward (Forward only).
adOpenKeyset 1 launches a keyset type of cursor.
adOpenDynamic 2 launches a dynamic-type cursor.
adOpenStatic 3 launches a static type of cursor.
----------------------------------

These cursor types will directly affect all properties and methods of the Recordset object, and the following list illustrates the differences between them.

PageCount does not support read-only read-only
PageSize can read, write, read, write, read and write
RecordCount does not support read-only read-only
Source can read, write, read, write, read and write
State read-only read-only read-only
Status read-only read-only read-only
AddNew Support Support
CancelBatch Support Support
CancelUpdate Support Support
Clone does not support unsupported
Close support support for support
Delete Support support for support
GetRows Support Support
Move does not support support support
MoveFirst Support Support
MoveLast support Support support is not supported
MoveNext Support Support
MovePrevious support Support support is not supported
NextRecordset Support Support
Open Support support supporting support
Requery Support Support Support support
Unsupported support support is not supported by Resync
Supports Support Support
Update Support support supporting support
UpdateBatch Support Support
----------------------------------

Where the NextRecordset method does not apply to Microsoft Access databases.
  
LockType

The LockType parameter of the Recordset object's Open method represents the lock type to take, and if this argument is omitted, then the system takes the LockType property of the Recordset object as the preset value. The LockType parameters include adLockReadOnly, adlockprssimistic, adLockOptimistic, and adLockBatchOptimistic, as described below:
  
--------------------------
Constants Constant Numerical description
--------------------------
adLockReadOnly 1 defaults, the Recordset object starts as read-only and cannot run AddNew, Update, and Delete methods

Adlockprssimistic 2 When the data source is being updated, the system temporarily locks the actions of other users to maintain data consistency.

adLockOptimistic 3 When the data source is being updated, the system does not lock other users ' actions, other users can add, delete, and change the operation of the data.

adLockBatchOptimistic 4 When the data source is being updated, other users must change the CursorLocation property to Adudeclientbatch to increase the data.

Delete, change the operation.

----------------------------------
Recordset Properties adOpenForwardOnly adOpenKeyset adopendynamic adOpenStatic
----------------------------------
AbsolutePage does not support read-write writable
AbsolutePosition does not support read-write writable
ActiveConnection can read, write, read, write, read and write
BOF read-only read-only read-only
Bookmark does not support read-write writable
CacheSize can read, write, read, write, read and write
CursorLocation can read, write, read, write, read and write
CursorType can read, write, read, write, read and write
EditMode read-only read-only read-only
EOF read-only read-only read-only
Filter can read, write, read, write, read and write
LockType can read, write, read, write, read and write
MarshalOptions can read, write, read, write, read and write
MaxRecords can read, write, read, write, read and write



Related Article

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.