1-hour ASP entry

Source: Internet
Author: User
Tags mdb database


<%
Statement
......
%>
<2> define the dim statement
<%
Dim a, B
A = 10
B = "OK !"
%>
Note: The Defined variables can be numeric, character, or other types.
<3> simple control process statements
1. If condition 1 then
Statement 1
Elseif condition 2 then
Statement 2
Else
Statement 3
Endif
2. While Condition
Statement
Wend
3. For Count = 1 to n step m
Statement 1
Exit
Statement 2
Next
Ii. Simple ASP database operation tutorial
<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 user. MDB data under the BBS \ db1 \ directory.
Library)
<%
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 that stores the data)
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 user. MDB data under the BBS \ db1 \ directory.
Library)
<%
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 that stores the data)
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 under 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 that stores the data)
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: whether the value of the Name field in the data table is equal to the value of the variable name "XX ".
Execute delete in combination,
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. Yes
Space 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
User field is character 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 matches
Num is equal, 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 and redirecting the URL,
Or set 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.

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.