6 large objects and database operations simple learning

Source: Internet
Author: User
Tags close close sessions root directory

ASP Learning steps:
1.5 Objects Request, Response, Session, Server, applictaion
2, two database components Adodb.recordset adodb.connection
3, Request.from ("form name") Get form data from
4, request.querystring ("url parameter") Get URL parameters
5, Index.asp?act=save
6, Request.QueryString ("act")

First, request
Request.Cookies ("Cookie") get cookies cookies generally used to store user information to verify sessions that are typically used for user authentication have the same type of difference is that the session exists on the server Cookies are available on the user's hard drive, which can be omitted directly using the request ("parameter name") He will use the from QueryString cookies in turn to fetch the value of the request it's pretty much it.

Second, Response
Response.Write Output to Browser
Response.Redirect, Heavy steering
Response.End stop exporting to the browser
Response.Cookies Write Cookies to client
Response.Cookies ("cookiename") = "Test"
Responsp.cookies ("CookieName"). Domain= "China228.com" is written in all china228.com domain names with request.cookies ("CookieName").

Third, session
Session ("sessionname") = "" Store information in session
If session ("SessionName") <> "" Then determine whether the session exists generally used to validate the session ("sessionname") = "" Empty session
Session.Abandon Clears all sessions
Session.Timeout = 100 Set session expiration unit to seconds
Session is the presence of cookies on the server that exist on the user's hard disk, the session is restarted, and the process pool is gone without cookies, and we can't control it unless you write a program that forces the empty

Four, Server
Server.MapPath the virtual path to the actual path, if your program is in C disk then you use Path = Server.MapPath ("/") Then and path = "C:" is the same based on the root directory
Server.CreateObject the more common Server.CreateObject ("Adodb.recordset") of this registration component to register a database component

Five applictaion  is a global object he and the session are the same difference between the session is the storage of individual user information application is the storage of global information
application ("Site") = "Http : All the files in this site can call this application  level two domain name, and you can use cookies to set up his available domain name
============ ===========================================
Six, adodb.connection connection database First registers a Conn object Set Conn = Server.CreateObject (" Adodb.connection ")  //Using the CreateObject method of the server object   then using the Connection Open method 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.mdb  under the current directory we put 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 defined as a variable, and ASP is a weakly typed language that is not defined or acceptable, unlike C # Java, which is defined before you can use
Execute Method    conn.execute (SQL) executes an SQL statement   Conn.execute (Insert into tablename (1,2,) Values (1,2) ")
Conn.close Close object   Insert inserts a data update modify data Delete select query


Vii. Adodb.recordset Returns a recordset first is also the registration object Set Rs = Server.CreateObject ("Adodb.recordset") this RS can be defined by itself not necessarily with RS or Conn because these are co. The M component (that is, a DLL written in DEIPHL C + +, some functionality that the ASP itself cannot implement) is not an ASP's built-in object, so you want to register the object

The recordset is then opened with the Open method Rs.Open "Select * from tablename", conn,1,3 rs.open SQL statement, Conn object, 1,3 (1,3 argument) SQL statement is generally a query statement Conn object is The database object opened earlier connection

1 cursor types to open the database
3 This parameter here, if you change to 1, you can not operate the library, can not update and delete (can not use Rs.addnew,rs.update,rs.delete) to 2 to open in exclusive form (when a user changes the library, will lock the library, keep the data consistency)
3 can go into any operation of the library, including delete modify update add
Here I usually use two kinds of pages in the list because you do not need to manipulate the library, read-only query on the line Rs.Open sql,conn,1,1 when adding modified data with Rs.Open sql,conn,1,3 Rs.bof whether the first data is return True instead Returns false Rs.fof whether the last data is return True and vice versa returns false that can be used to determine if there is a record in the database if rs.eof and Rs.bof then that the current cursor is not in the first or the last one. Record if Rs.eof then this indicates that there are records then we use a loop to interpret the data output RS and conn as the variables

Set Rs = Server.CreateObject ("Adodb.recordset")
Rs.Open sql,conn,1,1
If not rs.eof Then if it is not on the end of the recordset, there is a record
Do and not rs.eof until the end of the recordset jumps out of the loop
Response.Write Rs ("Field name")
Rs.movenext cursor moves down
Loop
End If
Rs.close//Logoff object
Set Rs = Nothing//Free resources

The type is not defined because the ASP is a weakly-typed quantitative object
If it's C # it's
int i; String str; ASP on Dim i,str and ASP Definition variables cannot be assigned an initial value

C # int i = 1;
ASP Dim i = 1

Let's talk about the pointer movement (on the above cursor, should be a pointer, the cursor is in C language, ASP does not)
Rs.movenext Move down one bar
Rs.moveprevious Move up one bar
Rs.movefirst move to the first one
Rs.movelast move to the last one.
Rs.absoluteposition = n Move record pointer to Nth line
The usual is Rs.movenext

Several method attributes of ASP paging
Rs.pagesize = N Show n data per page
Rs.absolutepage = n Moves the record pointer to the first piece of data on page n
Total number of records in a Rs.recordcount recordset
Total number of pages in the Rs.pagecount recordset

<%
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 ' show 20 per page
Curpage = request.querystring ("page") ' Gets the current number of pages
If curpage = "" or IsNumeric (curpage) or (Curpage-rs.pagecount) > 0 Then
' If curpage equals null or is not a numeric type or curpage is greater than total
Curpage = 1
' So curpage equals 1
End If
Rs.absolutepage = Curpage ' Set current Recordset page
i = 1
If not rs.eof Then if it is not on the end of the recordset, there is a record
Do as not rs.eof and I < PageSize if it is the last record in the dataset or I am already greater than rs.pagesize exit
Response.Write Rs ("Field name")
i = i + 1 per cycle once I + 1
Rs.movenext pointer moves down
Loop
End If
%>

<%if curpage=1 then%>
Home
<%else%>
<a href= "? page=1" > Home </a>
<%end if%>

<%if curpage=1 then%>
Previous page
<%else%>
<a href= "?page=<%=curpage-1%>" > Prev </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
<%else%>
<a href= "?page=<%=rs.pagecount%>" > Last </a>
<%end if%>

Description Curpage is the current page above with reqeust.querystring
Home:
This use of the current page is the first page when discriminant, if the current for the first page (that is, home), then display the first two words, no link, otherwise provide direct jump to the homepage of the link.
Previous page:
When you are currently on the first page, the link fails, and in turn, links to the previous page in front, where you use: <%=curpage-1%>, that is, by subtracting 1 from the current number of pages and getting the previous page.
Next page:
Here you need to use rs.pagecount this property to compare, if the total number of pages is less than the current page plus 1 of the value, which indicates that this is the next, the link will be invalidated, otherwise linked to the next page.
Last:
As with the function on the next page, the link is invalidated when the last page is found, otherwise the current page is specified as Rs.pagecount (total pages).

<%
Rs.close//Logoff object
Set Rs = Nothing//Free resources
%>

Rs.addnew This is a new record. The open dataset must be 1, 3

Set Rs = Server.CreateObject ("Adodb.recordset")
SQL = "SELECT * FROM Tealename"
Rs.Open sql,conn,1,3
Rs.addnew () Adding records to the end of the recordset
Rs ("Field name 1") = value 1
Rs ("Field Name 2") = value 2
Rs ("Field Name 3") = value 3
Rs.update () updates the modifications to the database
Rs.close
Set Rs = Nothing
%>
Rs.update Update Data

Set Rs = Server.CreateObject ("Adodb.recordset")
SQL = "SELECT * from tealename Where id = 1" To modify data with ID 1
Rs.Open sql,conn,1,3
Rs ("Field name to update 1") = Updated value 1
Rs.update () updates the modifications 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 downward, and if you want to delete more than one data, use a loop
Rs.close
Set Rs = Nothing

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.