<%@ Language=VBScript %> <% option explicit %> Simple DB Extraction

Simple Database Extraction

 

<% 'this page illustrates the simplest form of extracting data from a database 'key things to note-- '--the use of ADO for data access '--SQL for querying the database '--ASP and VBScript for working the presentation dim conn,rs dim fname, connstr,sql Set conn = Server.CreateObject("ADODB.Connection") 'the following assumes an ODBC connection is established ' conn.open "students","","" 'the following uses a more direct connection to the database fname = server.mappath("students.mdb") 'the following assumes a driver-based connection with the database connstr = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & fname conn.open connstr,"","" Set rs = Server.CreateObject("ADODB.Recordset") ' Substitute in form parameters into the query string sql = "select * from students order by lastname;" rs.Open sql, conn, 1, 1 'open the recordset, 1,1 is a read-only, forward retrieval do while not rs.eof Response.Write (rs.fields("lastname")) response.write (", ") Response.Write (rs.fields("firstname")) response.write (" ----- ") response.write ("ID field: " & rs.fields("studentid")) response.write ("

") 'Close the loop iterating records rs.MoveNext Loop rs.Close 'close the database as recordset to clean up the memory set rs = Nothing set conn = Nothing %>