Small sample ASP primer, very simple

Source: Internet
Author: User
Tags define contains include connect net variable trim access
<%
Statement
......
%>
<2> Define Variable Dim statement
<%
Dim a,b
a=10
b= "ok!"
%>
Note: The defined variable can be a numeric type, or it can be a character or other type of
<3> Simple Control Process statement
1. If Condition 1 Then
Statement 1
ElseIf Condition 2 Then
Statement 2
Else
Statement 3
endif
2.while conditions
Statement
Wend
3.for count=1 to n step m
Statement 1
Exit For
Statement 2
Next
Two. asp Database Simple * Tutorial
<1&gt. 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 in the bbs\db1\ directory)
<2> displaying database records
Principle: Display the record one by one in the database to the client browser, read out each record in the database in turn
If it's from beginning to end: Use loops and determine whether the pointer is used: not rs.eof
If it's from the end: Use the loop and determine if the pointer is starting to use: Not Rs.bof [from: Fly design net www.feitec.com]
<!--#include file=conn.asp--> (contains conn.asp to open the User.mdb database in the bbs\db1\ directory)
<%
Set Rs=server. CreateObject ("Adodb.recordset") (Create a Recordset object)
Sqlstr= "SELECT * from"----> (the message is a data table in the database, that is, the data table in which the data you want to display is stored)
Rs.Open sqlstr,conn,1,3----> (indicates how the database is opened)
Rs.movefirst----> (move pointer to first record)
While not rs.eof----> (judging whether the pointer is to the end)
Response.Write (RS ("name"))----> (Display the Name field in the data table message)
Rs.movenext----> (move pointer to next record)
Wend----> (end of Loop)
------------------------------------------------------
Rs.close
Conn.close These sentences are used to close the database
Set rs=nothing
Set conn=nothing
-------------------------------------------------------
%>
Where the response object is the information that the server sends to the client browser
<3> Increase database records
Add database records to rs.addnew,rs.update two functions
<!--#include file=conn.asp--> (contains conn.asp to open the User.mdb database in the bbs\db1\ directory)
<%
Set Rs=server. CreateObject ("Adodb.recordset") (Create a Recordset object)
Sqlstr= "SELECT * from"----> (the message is a data table in the database, that is, the data table in which the data you want to display is stored)
Rs.Open sqlstr,conn,1,3----> (indicates how the database is opened)
Rs.addnew Add a new record
RS ("name") = "XX" passes the value of XX to the Name field
Rs.update Refresh the database
------------------------------------------------------
Rs.close
Conn.close These sentences are used to close the database
Set rs=nothing
Set conn=nothing
-------------------------------------------------------[From: Soar design net www.feitec.com]
%>
<4> Delete a record
Deleting database records is mainly used to rs.delete,rs.update
<!--#include file=conn.asp--> (contains conn.asp 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"----> (the message is a data table in the database, that is, the data table in which the data you want to display is stored)
Rs.Open sqlstr,conn,1,3----> (indicates how the database is opened)
-------------------------------------------------------
While not rs.eof
If Rs. ("name") =name Then
Rs.delete
Rs.update the value of the Name field in the query datasheet is equal to the value "XX" of the variable name and, if so, performs the deletion,
else or continue the query until the pointer is to the end
Rs.movenext
EMD IF
Wend
------------------------------------------------------
------------------------------------------------------
Rs.close
Conn.close These sentences are used to close the database
Set rs=nothing
Set conn=nothing
-------------------------------------------------------
%>
<5> queries about the database
(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 (detects a space if the user value and pass value are null)
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 data table where the user field is a 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 ("Registered duplicate name")
%>
(b) The query field is a numeric type
<%
Dim num
Num=request. Form ("num")
Set Rs=server. CreateObject ("Adodb.recordset")
Sqlstr= "SELECT * from Message where id=" &num (the value of the ID field in the query message datasheet is equal to NUM, where the 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 ("Delete succeeded")
End If
Rs.close
Conn.close
Set rs=nothing
Set conn=nothing
Response.Write ("Delete failed")
%>
<6> explanation of a few simple ASP objects
Response object: A server-side information object sent to the client, including sending information directly to the browser, redirecting the URL, or setting the cookie value
Request object: Client requests to server
Session object: As a global variable, takes effect throughout the site
Server object: Provides access to methods and properties on the server
(a) General methods of use of response objects
Like what:
<%
Resposne.write ("Hello, Welcome to asp!")
%>
In the client browser you will see Hello, Welcome to asp! 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 a lot of uses for response objects, and we can study
General methods of use for Request objects
For example, the client requests to the server are passed through the request object.
For example: The personal information you fill out in your application for a mailbox is the object that will be used to
The information you fill out is delivered to the server
For example: This is a form of code, which is provided to the customer to fill out the information, completed by
"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= "Submission" value= "submitted" >
</p>
</form>
So request.asp how to read the information in, write to the database, where you need to use
Request object, below we will analyze the writing of request.asp
<%
Dim Name,password (Define user and password two variables)
Name=request.form ("User") (passes the user information from the form to the variable name)
Password=request.form ("Pass") (passes information in the form to the variable password)
%>
With the above code we read the data in the form, and the next thing we need to do is to
The information is written to the database, and the method of writing to the database is described above, not to be repeated here.
(through the above study everyone can make a message version of their own)

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.