Asp6 large object and Database Operations

Source: Internet
Author: User
Tags close close

ASP learning steps:
1. Five objects: request, response, session, server, and applictaion
2. Two database components, ADODB. recordset ADODB. Connection
3. Request. From ("form name") to get from form data
4. Request. querystring ("URL parameter") to obtain URL parameters
5. index. asp? Act = save
6. Request. querystring ("act ")

I. Request
Request. cookies ("cookies ") cookies are generally used to store user information for verification. sessions such as user verification are also used. The difference is that session exists on the server and cookies exist on the user's hard disk. directly use request ("parameter name ") he will use the from querystring cookies in sequence to get the request value.

Ii. Response
Response. Write output to the browser
Response. Redirect redirection
Response. End stop output to the browser
Response. Cookies write cookies to clients
Response. Cookies ("cookiename") = "test"
Responsp. Cookies ("cookiename"). Domain = "china228.com" can be obtained by using request. Cookies ("cookiename") under all china228.com domain names.

Iii. Session
Session ("sessionname") = "" stores information in the session
If SESSION ("sessionname") <> "" then determines whether a session exists. It is generally used to verify SESSION ("sessionname") = "" Clear session
Session. Abandon clear all sessions
Session. Timeout = 100 set the session expiration time in seconds
Session exists on the server. Cookies exist on the user's hard disk. If the session restarts the process pool and no cookies are available, we cannot control it unless a program is forcibly cleared.

Iv. Server
Server. mappath converts the virtual path to the actual path. If your program is on disk C, you can use Path = server. mappath ("/") is the same as Path = "C:/" based on the root directory.
Server. Createobject, a common component for registering a database, is server. Createobject ("ADODB. recordset ").

Applictaion is a global object. The difference between applictaion and session is that session stores a single user information. application stores global information.
Application ("Site") = "http: // www.webjx.com" as long as all files under the site can call this application second-level domain name, it cannot call this application, use cookies to set his available Domain Name
========================================================== ====================
Vi. ADODB. connection first registers a conn object set conn = server. createobject ("ADODB. connection ") // use the Createobject method of the server object and then use the open method of connection to connect to the database Conn. open "provider = Microsoft. jet. oledb.4.0; Data Source = "& server. mappath ("date. mdb ") server. mappath ("date. mdb ") is the date under the current directory. MDB generally puts this in conn. ASP
<%
Dim Conn, connstr
Set conn = server. Createobject ("ADODB. Connection ")
Connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("date. mdb ")
Conn. Open connstr
%>
Dim is used to define a variable. asp is a weak type language that does not need to be defined. Unlike C # Java, it must be defined first before it can be used.
Execute method conn. Execute (SQL) execute an SQL statement conn. Execute ("insert into tablename (,) values )")
Conn. Close close object insert a data update modify data delete SELECT query

VII. ADODB. recordset returns a record set, which is also the registration object set rs = server. createobject ("ADODB. recordset ") This RS can be defined by itself, not necessarily using RS or conn. Because these are COM components (that is, DLL written using deiphl C ++, some functions that ASP itself cannot implement) are not ASP's built-in objects, so you must register the objects.

Then we use the open method to open the record set Rs. open "select * From tablename", Conn, 1, 3 RS. open SQL statement, Conn object, 1, 3 (1, 3) SQL statement is generally a query statement. The conn object is the database object opened by the connection above.

1. Open the database cursor type
3. If this parameter is changed to 1, the database cannot be operated, and the database cannot be updated or deleted (RS cannot be used. addnew, Rs. update, Rs. delete) is changed to 2 and opened in exclusive form (when a user modifies the database, the database will be locked to maintain data consistency)
3. You can perform any operations on the database, including deleting, modifying, updating, and adding
Here, I usually use two types of RS for read-only queries because no database operation is required on the list page. open SQL, Conn, use Rs when adding and modifying data. open SQL, Conn, 1, 3 RS. if Bof is the first data, true is returned. Otherwise, false RS is returned. whether fof is the last piece of data is true. If false is returned, you can use this to determine whether the database records if RS. EOF and Rs. bof then indicates that if RS is not recorded if the cursor is neither in the first nor the last one. EOF then indicates that there is a record, so we will use a loop to output the data RS and conn, which is a variable.

Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open SQL, Conn, 1, 1
If not Rs. EOF then is not end-to-end in the record set, there is a record.
Do while not Rs. EOF until the record set finally jumps out of the loop
Response. Write RS ("field name ")
Rs. movenext cursor move down
Loop
End if
Rs. Close // deregister object
Set rs = nothing // release resources

Because ASP is a weak type quantitative object, you do not need to define the type.
If C # Is
Int I; string STR; ASP dim I, STR, and ASP variables cannot be assigned an initial value.

C # int I = 1;
ASP dim I = 1

Next let's talk about pointer Movement (the cursor above should be a pointer, the cursor is in the C language, ASP does not)
Rs. movenext move one down
Rs. moveprevious move one row up
Rs. movefirst move to the first entry
Rs. movelast move to the last one
Rs. absoluteposition = n move the record pointer to row n
Commonly used Rs. movenext

Several methods of ASP paging attributes
Rs. pagesize = n entries per page
Rs. absolutepage = n move the record pointer to the first data on page n
Rs. recordcount total number of records in the record set
Total number of pages in the RS. pagecount record set

<%
Dim Conn, connstr
Set conn = server. Createobject ("ADODB. Connection ")
Connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("date. mdb ")
Conn. Open connstr

Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open SQL, Conn, 1, 1
Pagesize = 20
Rs. pagesize = pagesize: 20 entries per page
Curpage = request. querystring ("page ")'
If curpage = "" Or isnumeric (curpage) or (curpage-Rs.pagecount)> 0 then
'If curpage is null or not a numerical value or curpage is greater than the total number
Curpage = 1
'Then curpage is equal to 1
End if
Rs. absolutepage = curpage 'sets the current record set page
I = 1
If not Rs. EOF then is not end-to-end in the record set, there is a record.
Do while not Rs. EOF and I <pagesize exit if it is already the last record of the dataset or if I is greater than Rs. pagesize
Response. Write RS ("field name ")
I = I + 1 each cycle I + 1
Rs. movenext pointer move down
Loop
End if
%>

<% If curpage = 1 then %>
Homepage
<% Else %>
<A href = "? Page = 1 "> homepage </a>
<% End if %>

<% If curpage = 1 then %>
Previous Page
<% Else %>
<A href = "? Page = curpage-1 %> "> previous </a>
<% End if %>

<% If Rs. pagecount <curpage + 1 then %>
Next Page
<% Else %>
<A href = "? Page = <% = curpage + 1%> "> next page </a>
<% End if %>

<% If Rs. pagecount <curpage + 1 then %>
Last page
<% Else %>
<A href = "? Page = <% = Rs. pagecount %> "> last page </a>
<% End if %>

It indicates that curpage is the current page obtained from reqeust. querystring.
Home page:
This parameter is used to determine whether the current page is the first page. If the current page is the first page (that is, the homepage), there is no link to the homepage. Otherwise, a link is provided to directly jump to the homepage.
Previous Page:
When the current is the first page, the link becomes invalid, in turn, link to the current Previous Page, here using: <% = curpage-1 %>, is to get the previous page with the current page minus 1.
Next page:
Here we need to use the Rs. pagecount attribute for comparison. If the total number of pages is less than the value of the current number plus 1, it indicates that this is the first page, and the link will be invalid; otherwise, it will be linked to the next page.
Last page:
The link is invalid when the last page is determined like the function of the next page. Otherwise, the current page is specified as Rs. pagecount (total number of pages ).

<%
Rs. Close // deregister object
Set rs = nothing // release resources
%>

Rs. addnew this is a new record to open the dataset must be 1, 3

Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * From tealename"
Rs. Open SQL, Conn, 1, 3
Rs. addnew () Add the record to the end of the record set
RS ("field name 1") = value 1
RS ("field name 2") = value 2
RS ("field name 3") = value 3
Rs. Update () updates the modification to the database.
Rs. Close
Set rs = nothing
%>
Rs. Update Data

Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * From tealename where id = 1" 'modify data with ID 1
Rs. Open SQL, Conn, 1, 3
RS ("field name to be updated 1") = updated value 1
Rs. Update () updates the modification to the database.
Rs. Close
Set rs = nothing
Rs. delete Delete

Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * From tealename where id = 1" 'Delete data with ID 1
Rs. Open SQL, Conn, 1, 3
Rs. Delete () 'deletes the current record, but the pointer does not move down. Loop is used to delete multiple data records.
Rs. Close
Set rs = nothing

Instructions on the operation object attributes of the two databases are complete.

 

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.