ASP database simple operation tutorial
<1>. Database connection (used to separate 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 USER.MDB database under BBS / DB1 / directory)
<2> Display Database Record
Principle: Display the record in the database to the client browser, read out each record in the database
If it is from the end to the end: use the loop and judge whether the pointer is used to use: Not r ..
If it is from the tail to the head: use the loop and judge whether the pointer is started using: NOT RS.BOF
(including conn.asp User.mdb data under 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, you want to display
Data stored in data)
RS.Open SQLSTR, CONN, 1, 3 ----> (indicating how to open the database)
rs.Movefirst ----> (will move the needle to the first record)
While Not Rs.eOf ----> (Judge whether the pointer is at the end)
Response.write (RS ("Name")) ----> (Displays the Name field in the data table message)
rs.movenext ----> (move the pointer to the next record)
Wend ----> (End of the loop)
-------------------------------------------------- ----
Rs.close
Conn.close This sentence is used to close the database
SET RS = Nothing
Set conn = Nothing
-------------------------------------------------- -----
%>
Where the Response object is the information sent to the client browser to the client browser
<3> Added Database Record
Add database records to rs.addnew, rs.Update two functions
(including conn.asp User.mdb data under 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, you want to display
Data stored in data)
RS.Open SQLSTR, CONN, 1, 3 ----> (indicating how to open the database)
Rs.AddNew adds a record
RS ("Name") = "xx" passes the value of the XX to the Name field
Rs.Update Refresh Database
-------------------------------------------------- ----
Rs.close
Conn.close This sentence is used to close the database
SET RS = NothingSet Conn = Nothing
-------------------------------------------------- -----
%>
. <4> Delete a record
Delete database records mainly use rs.delete, rs.Update
(including conn.asp User.mdb data under BBS / DB1 / directory
Library)
<%
DIM Name
Name = "xx"
SET RS = Server.createObject ("AdoDb.Recordset") (create a Recordset object)
SQLSTR = "Select * from message" ----> (Message is a data sheet in the database, that is, the data stored by the data you want to display)
RS.Open SQLSTR, CONN, 1, 3 ----> (indicating how to open the database)
-------------------------------------------------- -----
While Not Rs.eof
IF RS. ("Name") = Name Then
Rs.delete
rs.Update Query if the value of the Name field in the data table is equal to the value "XX" of the variable Name, if the execution is deleted
except,
Otherwise, continue to query until the pointer is until the end
rs.movenext
EMD IF
Wend
-------------------------------------------------- ----
-------------------------------------------------- ----
Rs.close
Conn.close This sentence is used to close the database
SET RS = Nothing
Set conn = Nothing
-------------------------------------------------- -----
%>
<5> Query about the database
(a) 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 "x" then (detects if the USER value and the pass value are empty, you can detect
To space)
Response.write ("Registration Information cannot be empty")
Else
SET RS = Server.createObject ("AdoDb.Recordset")
SQLSTR = "Select * from user where user = '" & user "(query the user in the user data table) User fields User
Field is a character type)
Rs.open SQLSTR, CONN, 1, 3
IF r.eof then
rs.addnew
RS ("User" = User
RS ("pass") = pass
RS ("QQ") = QQRS ("Mail") = mail
RS ("Message) = Message
Rs.Update
Rs.close
Conn.close
SET RS = Nothing
Set conn = Nothing
Response.write ("Register Success")
END IF
Rs.close
Conn.close
SET RS = Nothing
Set conn = Nothing
Response.write ("Registered Repetition")
%>
(b) Query field is digital
<%
DIM NUM
Num = Request.form ("NUM")
SET RS = Server.createObject ("AdoDb.Recordset")
SQLSTR = "SELECT * from message where id =" & num (Query if the value of the ID field in the message data table is
Num equal, where ID is digital)
Rs.open SQLSTR, CONN, 1, 3
IF not r.
Rs.delete
Rs.Update
Rs.close
Conn.close
SET RS = Nothing
Set conn = Nothing
Response.write ("Delete Success")
END IF
Rs.close
Conn.close
SET RS = Nothing
Set conn = Nothing
Response.write ("Delete Fail")
%>
%>