Tutorial | 10 Days Institute | data | database
The previous section: the establishment of a database
Learning Goals: Mastering Access database connections and reading records
First, we open the table in the database we created in our last lesson, and enter a few records, as shown below:
The content of this lesson is a bit boring, but it is very important. Here do not need to know the specific operation of the command, outside a lot of books are not suitable for the introduction is because of the introduction of too much theoretical knowledge, so that beginners confused.
Let's go straight to the bottom and look at two lines of code:
<%
Set Conn=server.createobject ("Adodb.connection")
Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Data/guestbook.mdb")
%>
The first line defines a ADODB database connection component, and the second line connects the database, so you can just change the name of the database below. Is it simple?
Look at three more lines below:
<%
Exec= "SELECT * FROM Guest"
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open exec,conn,1,1
%>
These three lines are added after the first two lines: Set the query Database command, the Select is followed by the field, if you want to query the words with *,from followed by the name of the table, we are not established in front of a gust table ah? Second sentence: Define a recordset component, all the search records are placed here, the third sentence is to open this Recordset, exec is the previous definition of the query command, Conn is the previous definition of the database connection component, the next parameter "1,1", which is read, after the modified record to set the parameter to 1, 3, OK. Next we read the record.
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" >
<%do While not rs.eof%><tr>
<td><%=rs ("name")%></td>
<td><%=rs ("tel")%></td>
<td><%=rs ("message")%></td>
<td><%=rs ("Time")%></td>
</tr><%
Rs.movenext
Loop
%>
</table>
The following three statements are used to close the database
Rs.close
Set rs=nothing
Conn.close
Set conn=nothing
In a table, we use 4 columns to display the last set of four fields, with Do loop, not rs.eof means that the condition is not read to the end of the recordset, rs.movenext means to show a go to the following record,<%=%> is equal to <%response.write%> used to insert ASP code inside the HTML code, mainly for displaying variables.
Copy and paste the above three pieces of code into the Clipboard, make sure to copy and paste in order! , and then save it for duqu.asp! Open the browser and enter in the Address bar: http://localhost/duqu.asp.
Here is the picture of the result above my machine.
Next section: writing a record