Learning Goals: Mastering Access database connections and reading records
What to learn today is a bit boring, but very important. Here we 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 see two words:
<%
Set Conn=server.createobject ("Adodb.connection")
Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Example3.mdb")
%>
The first sentence defines a ADODB database connection component, the second sentence is connected to the database, you just modify the database name on the back. Is it simple?
look at the next three sentences:
<%
Exec= "SELECT * FROM Guestbook"
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open exec,conn,1,1
%>
These three sentences are added to the first two sentences: Set the query Database command, the Select is followed by the field, if you want to query the words with the *,from followed by the name of the table, we are not established in front of a gustbook 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>
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.
Well, today is over, we can practice, you can download my example to see, debugging. Here's a picture of the results on my machine.