ASP learning steps:
1. 5 objects Request, Response, Session, Server, Application
2. Two database components Adodb.Recordset Adodb.Connection
3. Request.From(form name) to get From form data
4. Request.QueryString(Url parameter) Get the Url parameter
5. index.asp?act=save
6. Request.QueryString(act)
1. request
Request.Cookies(Cookie) Get Cookies Cookies are generally used to store user information and are used for verification. Sessions also have the same type. The difference is that Sessions are stored on the server. Cookies are stored on the user's hard disk. Generally, you can omit the method directly. Using Request (parameter name), he will use From QueryString Cookies to get the value of Request. That’s almost it.
2. Response
Response.Write output to the browser
Response.Redirect redirection
Response.End stops output to the browser
Response.Cookies writes cookies to the client
Response.Cookies(CookieName) = test
Responsp.Cookies(CookieName).Domain=china228.com If written like this, it can be obtained by using Request.Cookies(CookieName) under all China228.com domain names.
3. Session
Session(SessionName) = Store information in Session
If Session(SessionName) <> Then Determining whether Session exists is generally used to verify Session(SessionName) = Clear Session
Session.Abandon clears all Sessions
Session.TimeOut = 100 Set Session expiration time unit to seconds
Session is stored on the server. Cookies are stored on the user's hard disk. When the session is restarted, the process pool will be gone and there will be no more cookies. We cannot control it unless we write a program to force clear it.
4. Server
Server.Mappath converts the virtual path to an actual path. If your program is on the C drive, then you use path = Server.Mappath(/), which is the same as path = c:/ based on the root directory.
Server.CreateObject is a commonly used registration component. Server.CreateObject(Adodb.RecordSet) registers a database component.
5. Application is a global object. It is the same as Session. The difference is that Session stores individual user information. Application stores global information.
Application(Site) = Http://www.webjx.com As long as all files under the site can call this Application, the second-level domain name cannot call this Application, but use Cookies to set its available domain name.
================================================== =====
6. Adodb.Connection To connect to the database, first register a Conn object Set Conn = Server.CreateObject(Adodb.Connection) //Use the Server object's CreateObject method and then use the Connection's 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 Date.Mdb under the current directory. Generally, 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 to define a variable, asp is a weakly typed language and does not need to be defined, unlike C#, Java and the like which must be defined before it can be used.
Execute method Conn.Execute(Sql) executes a SQL statement Conn.Execute(Insert Into Tablename(1,2,)Values(1,2))
Conn.Close closes the object insert insert a piece of data update modify data delete delete select query
7. Adodb.Recordset returns a record set, which is also a registered object Set Rs = Server.CreateObject(Adodb.Recordset). This rs can be defined by yourself. It is not necessary to use rs or conn because these are Com components (that is, use deiphl C++ Dll written, some functions that ASP itself cannot implement) are not built-in objects of ASP, so the object must be registered
Then also 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 parameters will be discussed later) The Sql statement is generally a query statement The Conn object is the database object opened by the Connection above.
1 Open the cursor type of the database
3 If this parameter is changed to 1, the library cannot be operated, updated or deleted (Rs.Addnew, Rs.Update, Rs.Delete cannot be used). If it is changed to 2, it will be opened in an exclusive form (when a user is modifying it) When accessing the library, the library will be locked to maintain data consistency)
3. You can perform any operation on the library, including deleting, modifying, updating, and adding.
Generally, I will use two types here. When the list page is used, because there is no need to operate the library, read-only query is enough. Rs.Open Sql,Conn,1,1. When adding and modifying data, I use Rs.Open Sql,Conn,1,3 Rs. .Bof returns True if it is the first piece of data, otherwise it returns False. Rs.Fof returns True if it is the last piece of data, otherwise it returns False. You can use this to determine whether there is a record in the database if rs.eof and rs.bof then means that if the current cursor is neither the first nor the last item, it means there is no record. if rs.eof then means there is a record, then we will use a loop to output the data. Rs is understood the same as Conn, it is a variable.
Set Rs = Server.CreateObject(Adodb.Recordset)
Rs.Open Sql,Conn,1,1
If Not Rs.Eof Then If it is not at the end of the record set, it means there is a record
Do While Not Rs.Eof until the end of the record set breaks out of the loop
Response.Write Rs(field name)
Rs.MoveNext Cursor moves down
Loop
End If
Rs.Close //Unregister the object
Set Rs = Nothing //Release resources
Because asp is a weakly typed quantitative object, there is no need to define the type.
If it's C#, that's it
int i; string str; asp is dim i, str and asp defines variables that cannot be assigned initial values.
C# int i = 1;
aspdimii=1
Let’s talk about pointer movement (the cursor above should be a pointer, the cursor is in C language, not in ASP)
Rs.MoveNext Move down one line
Rs.MovePrevious Move up one line
Rs.Movefirst Move to the first item
Rs.MoveLast Move to the last item
Rs.Absoluteposition = n moves the record pointer to the nth row
The commonly used one is Rs.MoveNext
Talk about several method attributes of asp paging
Rs.Pagesize = n Display N pieces of data per page
Rs.absolutepage = n will move the record pointer to the first data on page N
Rs.RecordCount The total number of records in the record set
Rs.PageCount The total number of pages in the 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 'Display 20 items per page
curpage = Request.QueryString(Page) 'Get the current page number
If curpage = Or IsNumeric(curpage) Or (curpage-Rs.pagecount) > 0 Then
'If curpage is equal to empty or not a numeric type or curpage is greater than the total number
curpage=1
'Then curpage is equal to 1
End If
Rs.absolutepage = curpage 'Set the current recordset page
i=1
If Not Rs.Eof Then If it is not at the end of the record set, it means there is a record
Do While Not Rs.Eof and i < PageSize Exit if it is the last record of the data set or if i is greater than Rs.PageSize
Response.Write Rs(field name)
i = i + 1 i + 1 once every loop
Rs.MoveNext pointer moves down
Loop
End If
%>
<%if curpage=1 then%>
front page
<%else%>
<a href=?page=1>Homepage</a>
<%end if%>
<%if curpage=1 then%>
Previous page
<%else%>
<a href=?page=<%=curpage-1%>>Previous page</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%>
Note that curpage is the current page obtained using Reqeust.Querystring above.
front page:
This is determined by whether the current page is the first page. If the current page is the first page (that is, the home page), then the word home page will be displayed without a link. Otherwise, a link will be provided to jump directly to the home page.
Previous page:
When the current page is the first page, the link is invalid. In turn, the link is to the previous page. Here we use: <%=curpage-1%>, which means subtracting 1 from the current page number to get the previous page.
Next page:
Here you need to use the rs.pagecount attribute for comparison. If the total number of pages is less than the current page number plus 1, it means that this is the last page and the link will be invalid. Otherwise, the link will be to the next page.
Last page:
The same function as the next page determines that the link will be invalid when it is the last page, otherwise the current page will be designated as rs.pagecount (total page count).
<%
Rs.Close //Unregister the object
Set Rs = Nothing //Release resources
%>
Rs.Addnew This is to add a new record. The open data set must be 1,3
Set Rs = Server.CreateObject(Adodb.Recordset)
Sql = Select * From Tealename
Rs.Open Sql,Conn,1,3
Rs.Addnew() adds 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 changes 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 'Modify the data with id 1
Rs.Open Sql,Conn,1,3
Rs (field name to be updated 1) = updated value 1
Rs.Update() updates changes 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 the data with id 1
Rs.Open Sql,Conn,1,3
Rs.Delete() 'Delete the current record, but the pointer will not move downward, so if you want to delete multiple pieces of data, you need to use a loop
Rs.Close
Set Rs=Nothing
The lecture on the properties of the two major database operation objects is completed.