An Introduction To ADO
K. Yue, copyright 2000
Created November 1, 2000

Introduction

VBScript

<!--#include File="adovbs.inc"-->

Dim objResult
Set objResult = Server.CreateObject( "ADODB.Recordset" )

ADO Objects

The Connection Object

set Conn = Server.CreateObject("ADODB.Connection")

' No password
Conn.open "Resume"
' With username yue and password sleeping.
Conn.open "Resume", "yue", "sleeping"
' Alternatively,
Conn.open "DSN=Resume; UID=yue; password=sleeping"

conn.ConnectionString = "driver={SQL Server};" & _
    "server=voyager;uid=someid;pwd=somepwd;database=Voyager"
conn.ConnectionTimeout = 30
conn.Open

sql = "select * from Students"
Set ResultsRS = Conn.Execute(SQL)

Conn.BeginTrans ' Begin transaction.
SQL = "Delete * from Faculty where " & _
      "FacultyID = '100000000'"
Conn.Execute SQL, nRecords
TotalErrors = Conn.Errors.Count
SQL = "Delete * from FacultyReference " & _
      "where FacultyID = '100000000'"
Conn.Execute SQL, nRecords
TotalErrors = TotalErrors + Conn.Errors.Count

If TotalErrors = o Then
   Conn.CommitTrans
Else
   Conn.RollbackTrans
End If

The RecordSet Object

Set rstSQ = Server.CreateObject("ADODB.Recordset")
Set rstSQ.ActiveConnection = conn

rstSQ.CursorType = adOpenStatic

'   open the table Student
rstSQ.open = "Student"
'   execute a SQL query.
rstSQ.open "select * from Student"

Use Move, MoveNext and MoveFirst to move the cursor.

Example:

Visit Count for each visiting IP address. Assume that the table IPAddresses(IPAddress, Count) exists in the DSN IPAddresses.

<html>
<head>
<title>Visit Counts per ip addresses</title>
</head>
<body>

<%
'   Get Remote IP Address.
IPAddress = Request.ServerVariables("Remote_Addr")
'   Open database connection.
Set conn = Server.CreateObject("adodb.connection")
conn.open "IPAddresses"

'   Check to see the IP address exits.
'   Inefficient: to be modified.
sql = "select * from IPAddresses"
set result = conn.execute(sql)
found = false
Do while (not result.eof)
   if (IPAddress = result.fields.item("IPAddress")) then
      Count = Cint(result.fields.item("Count")) + 1
      found = true
     exit Do
   end if
   result.movenext
loop

if found then
   sql = "update IPAddresses set Count = " & Count & _
      " where IPAddress = '" & IPAddress & "'"
   conn.execute(sql)
else
   Count = 1
   sql = "insert into IPAddresses values('" & _
   IPAddress & "', " & Count & ")"
   conn.execute(sql)
end if
conn.close

'    Print result.
Response.write("IP Address " & IPAddress & " has visited " & _
"this page for " & count & " times.")

%>
</body>
</html>