<1>. Database Connection (used to separate the connection file conn. asp)
<%
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & server. mappath ("/BBS/db1/user. mdb ")
%>
(Used to connect the user. mdb database under the BBS/db1/directory)
<2> display database records
Principle: displays the records in the database one by one in the client browser, reads each record in the database in sequence
If it is from start to end: Use a loop and determine whether the pointer is used until the end: not Rs. EOF
If it is from the end to the header: Use a loop and determine whether the pointer is used to start using: not Rs. bof
<! -- # Include file = conn. asp --> (conn. asp is used to open the user. mdb database in the BBS/db1/directory)
<%
Set rs = server. Createobject ("ADODB. recordset") (Create A recordset object)
Sqlstr = "select * From message" ----> (message is a data table in the database, that is, the data table where the data you want to display is stored)
Rs. Open sqlstr, Conn, 1, 3 ----> (indicates how to open the database)
Rs. movefirst ----> (move the pointer to the first record)
While not Rs. EOF ----> (judge whether the pointer ends)
Response. Write (RS ("name") ----> (display the name field in the message table)
Rs. movenext ----> (move the pointer to the next record)
Wend ----> (loop end)
------------------------------------------------------
Rs. Close
Conn. Close is used to close the database.
Set rs = nothing
Set conn = nothing
-------------------------------------------------------
%>
The response object is the information sent by the server to the client browser.
<3> Add database records
Rs. addnew and Rs. Update functions are used to add database records.
<! -- # Include file = conn. asp --> (conn. asp is used to open the user. mdb database in the BBS/db1/directory)
<%
Set rs = server. Createobject ("ADODB. recordset") (Create A recordset object)
Sqlstr = "select * From message" ----> (message is a data table in the database, that is, the data table where the data you want to display is stored)
Rs. Open sqlstr, Conn, 1, 3 ----> (indicates how to open the database)
Rs. addnew adds a new record
RS ("name") = "XX" pass the value of XX to the name field
Rs. Update refresh Database
------------------------------------------------------
Rs. Close
Conn. Close is used to close the database.
Set rs = nothing
Set conn = nothing
-------------------------------------------------------
%>
<4> delete a record
Rs. Delete and Rs. Update are used to delete database records.
<! -- # Include file = conn. asp --> (conn. asp is used to open the user. mdb database in the BBS/db1/directory)
<%
Dim name
Name = "XX"
Set rs = server. Createobject ("ADODB. recordset") (Create A recordset object)
Sqlstr = "select * From message" ----> (message is a data table in the database, that is, the data table where the data you want to display is stored)
Rs. Open sqlstr, Conn, 1, 3 ----> (indicates how to open the database)
-------------------------------------------------------
While not Rs. EOF
If Rs. ("name") = Name then
Rs. Delete
Rs. Update: query whether the value of the Name field in the data table is equal to the value of the variable name "XX". If yes, delete the field,
Else otherwise, the query continues until the pointer ends.
Rs. movenext
EMD if
Wend
------------------------------------------------------
------------------------------------------------------
Rs. Close
Conn. Close is used to close the database.
Set rs = nothing
Set conn = nothing
-------------------------------------------------------
%>
<5> query Databases
(A) the query field is character-type.
<%
Dim user, pass, QQ, mail, message
User = request. Form ("user ")
Pass = request. Form ("pass ")
Qq = request. Form ("QQ ")
Mail = request. Form ("mail ")
Message = request. Form ("message ")
If trim (User) & "X" = "X" or trim (PASS) & "X" = "X" then (check whether the user and pass values are empty, space can be detected)
Response. Write ("registration information cannot be blank ")
Else
Set rs = server. Createobject ("ADODB. recordset ")
Sqlstr = "select * from user where user = '" & user & "'" (query the user field in the user data table, where the user field is of the Role type)
Rs. Open sqlstr, Conn, 1, 3
If Rs. EOF then
Rs. addnew
RS ("user") = user
RS ("pass") = pass
RS ("QQ") = QQ
RS ("mail") = Mail
RS ("message") = message
Rs. Update
Rs. Close
Conn. Close
Set rs = nothing
Set conn = nothing
Response. Write ("registration successful ")
End if
Rs. Close
Conn. Close
Set rs = nothing
Set conn = nothing
Response. Write ("register duplicate names ")
%>
(B) the query field is Numeric.
<%
Dim num
Num = request. Form ("num ")
Set rs = server. Createobject ("ADODB. recordset ")
Sqlstr = "select * From message where id =" & num (query whether the value of the ID field in the message data table is equal to that of num, where ID is Numeric)
Rs. Open sqlstr, Conn, 1, 3
If not Rs. EOF then
Rs. Delete
Rs. Update
Rs. Close
Conn. Close
Set rs = nothing
Set conn = nothing
Response. Write ("deleted successfully ")
End if
Rs. Close
Conn. Close
Set rs = nothing
Set conn = nothing
Response. Write ("deletion failed ")
%>
<6> several simple ASP objects
Response object: The information object sent by the server to the client, including sending the information directly to the browser, redirecting the URL, or setting the cookie value.
Request object: the client sends a request to the server.
Session Object: as a global variable, it takes effect for the whole site
Server Object: provides access to the server's top-level and attribute
(A) Common usage of response objects
For example:
<%
Resposne. Write ("Hello, welcome to ASP! ")
%>
Hello, welcome to ASP will be displayed in the client browser! This paragraph of Text
<%
Response. Redirect ("www.sohu.com ")
%>
If you execute this section, the browser will automatically connect to the "Sohu" url
There are many other methods for using response objects. You can study them.
Common Request object usage
For example, the client sends a request to the server through the request object.
For example, the personal information you entered in the application email address is
The information you entered is transmitted to the server
For example, this is a piece of form code, which is provided to the customer to fill in the information.
"Submit" is passed to the request. asp file for processing and then stored in the server database
<Form name = "form1" method = "Post" Action = "request. asp">
<P>
<Input type = "text" name = "user">
</P>
<P>
<Input type = "text" name = "pass">
</P>
<P>
<Input type = "Submit" name = "Submit" value = "Submit">
</P>
</Form>
How to read the information in request. asp is required here when writing data to the database?
Request object. Next we will analyze the method of request. asp.
<%
Dim name, password (two variables are defined: user and password)
Name = request. Form ("user") (pass the user information in the form to the variable name)
Password = request. Form ("pass") (pass the pass information in the form to the variable password)
%>
With the above Code, we will read the data in the form. Next we will do the following:
The information is written to the database. The methods used to write data to the database are described above. I will not repeat them here.