28
Feb
Simple Table Display (Select Fields)
Displaying data from an Access or SQL Server database is easy to accomplish in .asp. The following example utilizes our Access database of Mark 'The Bird' Fidrych and selects specific season statistics contained in the database for display based upon what appears in the select statement in our sql query. A query utilizing select * will bring across every table element and if every element is not needed resource has been wasted. In addition select will not allow grouping function which may be critical for some applications.
CodeAve.com(Display Table with Select Fields(Hard-Coded))
<%
' Name of the Accessdb being read
accessdb="fidrych"
' Connect to the db with a DSN-less connection
cn="DRIVER={Microsoft Access Driver (*.mdb)};"
cn=cn & "DBQ=" & server.mappath(accessdb)
' Create a server recordset object
Set rs = Server.CreateObject("ADODB.Recordset")
' Select specific fields from the table the_bird
sql = "select year, team, w, l, cg, so, era from the_bird"
' Execute the sql
rs.Open sql, cn
%>
| Year | Team | W | L | CG | SO | ERA |
<%= rs("year") %> | <%= rs("team") %> | <%= rs("w") %> | <%= rs("l") %> | <%= rs("cg") %> | <%= rs("so") %> | <%= rs("era") %> |
<%
' Close and set the recordset to nothing
rs.close
set rs=nothing
%>
