ActiveX Data Objects
ActiveX Data Objects
CS-422
Dick Steflik
ActiveX Data Objects (ADO)
• Does for Microsoft data sources what ODBC did for databases
– similar in concept to the combination JDBC and JNDI (and then some)
• The process of using ADO involves the four step process:
– Connect to the database
– Define the data you want
– Manipulate the data
– Display the data
ADO Software Architecture
ADO
OLE DB
ADO
ODBC
JET SQL Oracle
Drivers
Dim cnn
Dim str str = “Provider=MSDASQL; Data Source=mydatasource; User Id=;Password=“
cnn.ConnectionString = str
Open() and Close()
• Once the connection has been established the Connection must be
opened before using it
• Likewise it should be closed when done.
Dim cnn
Dim str
Set cnn = CreateObject(‘ADODB.Connection’)
str = “Provider=MSDASQL.1,Data Source=mydatasource;”
str = str & User TD=myuserid;Password=mypass”
cnn.ConnectionString = str
cnn.Open
…..
cnn.Close
Recordset
• Record set objects are the recipients of the result of a SQL query
• create the Connection object the create a recordset object and associate
them
Dim cnn
Dim rs
set cnn = CreateObject(“ADODB.Connection”)
set rs = CreateObject(“ADODB.Recordset”)
‘ assocoate the record set with the connection
rs.ActiveConnection = cnn
To use the Recordset, Open it
Dim string
While Not rs.EOF
string = string & rs(“CourseName”) & “<br>”
rs.MoveNext
Wend
Getting data in
• To get data into an ADO data source use the ADO command object.
• Use this for the SQL
– insert
– update
– and delete statements
Dim SQL
Dim Cmd
Set cmd = CreateObject(“ADODB.Command”) ‘create a command object
SQL = “INSERT into …..”
cmd.CommandText = SQL ‘set the CommandTexy property of the CommandObject
cmd.ActiveConnection = cnn
cmd.Execute
Display records from a database in an HTML Page
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", conn
do until rs.EOF
for each x in rs.Fields
Response.Write(x.name)
Response.Write(" = ")
Response.Write(x.value & "<br />")
next
Response.Write("<br />")
rs.MoveNext
loop
rs.close
conn.close
%>
</body>
</html>
Put the data in a table with colors and titles
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM Customers"
rs.Open sql, conn
%>
<table border="1" width="100%" bgcolor="#fff5ee">
<tr>
<%for each x in rs.Fields
response.write("<th align='left' bgcolor='#b0c4de'>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
</table>
</body>
</html>
Another example, make data on page sortable by user
<html>
<body>
<table border="1" width="100%">
<tr>
<td><a href="demo_sort2.asp?sort=companyname">Company</a></td>
<td><a href="demo_sort2.asp?sort=contactname">Contact Name</a></td>
</tr>
<%
dim conn,rs,sort
sort="companyname"
if Request.QueryString("sort")<>"" then
sort=Request.QueryString("sort")
end if
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("northwind.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "SELECT Companyname,Contactname FROM Customers ORDER BY " & sort,conn
do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%> </td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
</table>
</body>
</html>
Using the Recordset Object This example returns the value of the first column in the first two records:
ALFKI
ANTON
This example returns the value of the first three columns in the first record:
ALFKI
<body> Alfreds Futterkiste
<% Maria Anders
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", conn
'The first number indicates how many records to copy
'The second number indicates what recordnumber to start on
p=rs.GetRows(2,0)
response.write("<p>This example returns the value of the first column in the first two records:</p>")
response.write(p(0,0))
response.write("<br>")
response.write(p(0,1))
response.write("<p>This example returns the value of the first three columns in the first record:</p>")
response.write(p(0,0))
response.write("<br>")
response.write(p(1,0))
response.write("<br>")
response.write(p(2,0))
rs.close
conn.close
%>
</body>
</html>
ADO.NET
• Under .NET, ADO hasn’t changed much but the way you use it has
– VB has changed extensively under .NET so we won’t address its use with
VB (developer community isn’t real happy about this)
– the ADO API is pretty consistent across the MS supported .NET
languages
• Consists of the namespaces and classes that manage accessing
backend databases.
– five namespaces
• the most common are:
– Ststem.Data
– System.Data.OleDB
– System.Data.SqlClient
– hundreds of classes
the .NET Data Provider
.NET Data Provider
DataReader
Connection Command
Transaction Parameters
DB
SelectCommand UpdateCommand
InmsertCommand DeleteCommand
.NET DataSet
DataSet
DataTableCollection
DataTable
DataRowCollection
DataColumnCollection
ConstraintCollection
DataRelationshipCollection
System.Data Namespace