Web Developer 2

ActiveX Data Objects (ADO) Overview

What is ADO?


Accessing a Database from an ASP Page

The common way to access a database from inside an ASP page is to:

  1. Create an ADO connection to a database
  2. Open the database connection
  3. Create an ADO recordset
  4. Open the recordset
  5. Extract the data you need from the recordset
  6. Close the recordset
  7. Close the connection

Create a DSN-less Database Connection

The easiest way to connect to a database is to use a DSN-less connection. A DSN-less connection can be used against any Microsoft Access database on your web site.

If you have a database called "bookstore2000.mdb" located in a web directory like "db", you can connect to the database with the following ASP code:

var con = Server.CreateObject("ADODB.Connection");

con.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath("\\db\\bookstore2000.mdb"));

Note, from the example above, that you have to specify the Microsoft Access database driver as well as a Database Qualifier (DBQ) that points to the .mdb file on the server your.  The above uses the MapPath() method of the Server object to dynamically point to the database.


Create an ODBC Database Connection

If you have an ODBC data source called "bookstore2000" you can connect to the database with the following ASP code:

<%
var con=Server.CreateObject("ADODB.Connection"); 
conn.Open("bookstore2000");
%>

With an ODBC connection, you can connect to any database, on any computer in your network, as long as an ODBC connection is available

The ADO Connection Object

The ADO Connection object is used to create an open connection to a data source. Through this connection, you can access and manipulate a database.  Think of the Connection object as a pipeline of data between your application and the data in the database.  As with most pipelines,  data can flow in both directions.

Create an ADO Table Recordset

After an ADO Database Connection has been created, as demonstrated in the previous section, it is possible to create an ADO Recordset.  

Suppose we have a database named "bookstore2000", we can get access to the "Customers" table inside the database with the following lines:

<%

var con = Server.CreateObject("ADODB.Connection");

con.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath("\\db\\bookstore2000.mdb"));
var rs=Server.CreateObject("ADODB.recordset");
rs.Open("Customers", con);
%>

 

Create an ADO SQL Recordset

We can also get access to the data in the "Customers" table using SQL:

<%

var con = Server.CreateObject("ADODB.Connection");

con.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath("\\db\\bookstore2000.mdb"));
var rs=Server.CreateObject("ADODB.RecordSet");
rs.Open("Select * from Customers", con);
%>

 

Extract Data from the Recordset

After a recordset is opened, we can extract data from recordset.  

Suppose we have a database named "bookstore2000", we can get access to the "books" table inside the database with the following lines and then write out the ISBN and Title of each book:

<%

var con = Server.CreateObject("ADODB.Connection");

con.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath("\\db\\bookstore2000.mdb"));
var rs = Server.CreateObject(“ADODB.RecordSet”);
rs.Open("Select * from books", con);
<%
while(!rs.EOF){
Response.Write(rs("ISBN"));
Response.Write(‘&nbsp;’);
Response.Write(rs(“Title”);
Response.Write("<br>");
rs.MoveNext();
}
%>
%>

The above makes use of the EOF property of the RecordSet object.  Essentially, the code says: loop through the RecordSet until you run out of records and display the ISBN and Title of each record in the RecordSet.

The ADO Recordset Object

The ADO Recordset object is used to hold a set of records from a database table. 

Display Records in an HTML Table

Displaying the records created above in an HTML table is straightforward:

<table border="0" cellpadding="6">

<tr>

      <th width="75">ISBN</th>

      <th width="50%">Title</th>

</tr>

<%

while(!rs.EOF){

%>

<tr>

<td><%= rs("ISBN") %></td>

<td><%= rs("Title") %></td>

</tr>

<%

rs.MoveNext();

}

%>

</table>

The above code creates a fixed row for the headers and then a dynamic row that is populated by looping through each of the records in the recordset.  I hope you are beginning to see the power of ASP – imagine having to create static HTML pages for the above list!  What would happen when the list changes (as it almost certainly would on a regular basis)?  With ASP, changes to the database are automatically reflected on the web!

Also note that you can inter-mix ASP code within your HTML almost any way you'd like.  You can even write out static HTML - as above - within a loop where the loop exists in different code blocks.  That's powerful!