Using Databases with Webclasses

You can use the ADO features of Visual Basic to retrieve and manipulate information stored in a database in response to user requests. Using webclasses in this way can help you create a powerful application. With ADO, you can open a connection to the database, build recordsets, retrieve data, and update information in the database.

Note   If you are working with a database, you must have the appropriate database program on your Web server. In addition, you should make use of ODBC connection pooling and ADO disconnected recordsets when accessing a database. For more information on these features, see the ActiveX Data Objects documentation in your MSDN library.

There are multiple ways you might use databases in your applications. For example, you might create an application in which a series of images for a catalog are stored in a database. When users in your IIS application select a link from the catalog table of contents, the webclass can intercept that link, open a connection to the database, retrieve the image, and return it to the browser. Or you might use a search page to query a database for matching items and display the results in a generated table.

You can use the Respond event or another event for the appropriate webitem to handle your database connection and processing. For example, the following code shows how you would open a connection to an ADO database, create a recordset, and retrieve information from it:

Private Sub AuthorList_Respond()

   'Declare object variables for the database connection and recordset
   Dim cn As New ADODB.Connection
   Dim rs As New ADODB.Recordset

'Open the database connectioncn.ConnectionString = "DSN=csmith;UID=sa;PWD=sa;DATABASE=pubs"cn.Open
'Create the recordsetrs.Open "select * from customer", cn, adOpenStatic, adLockReadOnly

   'Write resulting information to a table, record by record
   With Response
      .Write "<HTML>"
      .Write "<BODY>"
      .Write "<TABLE BORDER CELLSPACING=1 CELLPADDING=7>"

      Do While rs.EOF = False
         .Write "<TR><TD>"
         .Write rs("authorlast") & ", " & rs("authorfirst")
         .Write "</TD><TD>"
         .Write rs("title")
         .Write "</TD><TD>"
         .Write rs("publisher")
         .Write "</TD><TR>"
         rs.MoveNext
      Loop
      .Write "</TABLE>"
      .Write "</BODY>"
      .Write "</HTML>"

   End With

   'Close the recordset and the database connection
   rs.Close
   cn.Close
  
End Sub

For More Information   For more information on ADO processing, search for "ActiveX Data Objects" in the MSDN Library.