The ADO object model is implemented as an OLE automation (ActiveX) DLL. This means ADO objects are programmable in any language that supports COM and OLE automation DLLs, such as Visual Basic, VBScript, Visual C++, and Java.
ADO is cross language, which means it is consistent among different programming languages. ADO is freely redistributable. Because ADO is built on top of OLE DB, OLE DB components are required for redistribution. If the ODBC Provider is used as a provider, the ODBC Provider must be redistributed as well.
Visual Basic applications can create an ADO object at design time or run time. ADO objects such as OLE Automation DLLs can be used as built-in objects in Visual Basic at design time. You can find out if these DLLs exist by clicking References on the Project menu. ADO objects can be declared using the Dim statement.
The example shows specifying ADO objects at design time:
Dim cn as New ADODB.Connection
Dim cmd as New ADODB.Command
Dim rs as New ADODB.Recordset
All the examples shown in the ADO object model section use this approach.
If the existence of the OLE Automation DLLs is not recognized at design time, you can create objects (classes) that are inside OLE Automation DLLs at run time. The CreateObject function creates an object at run time. The class of the object is passed as a String argument to CreateObject. The class names can be found using the Object Browser.
ADO createable object classes are: ADODB.Connection, ADODB.Command, and ADODB.Recordset.
This example shows creating an ADO object at run time specifying its class:
Set rs=CreateObject("ADODB.Recordset")
ADO can be used in VBScript language inside HTML or Active Server Pages. VBScript code is almost identical to Visual Basic code. For VBScript code to work in an HTML or Active Server Pages, the code must be inserted within a pair of HTML tags or under the Scripting section.
VBScript cannot load ADO constants from the ADO type library; therefore, unless the ADO constants declaration file is included in VBScript code, the literal values of ADO constants must be specified. The Adovbs.inc file contains all the ADO constant definitions and can be included in your script.
To use ADO objects in VBScript, first include the Adovbs.inc file, and then use the CreateObject method to create ADO objects. The Adovbs.inc file can be found in the OLE DB SDK redist directory.
The example shows using ADO in a VB Scripting Web page to list customers' first and last names in a table:
<%@ LANGUAGE = VBScript %>
<HTML>
<TITLE>Using ADO in a Visual Basic Script Web Page</TITLE>
</HEAD>
<LANGUAGE="VBS">
<!--#include file="adovbs.inc"-->
<CENTER>
<H1><font size=4>Using ADO in a Visual Basic Script Web Page</H1></font><br><br>
<%set myConnection = CreateObject("ADODB.Connection")
myConnection.Open "DSN=pubs;UID=sa"
SQLQuery = "select title_id,title from titles"
set RSTitleList = myConnection.Execute(SQLQuery)%>
<TABLE align=center COLSPAN=8 CELLPADDING=5 BORDER=0 WIDTH=200>
<!-- BEGIN column header row -->
<TR>
<TD VALIGN=TOP BGCOLOR="#800000">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>
Title ID</FONT>
</TD>
<TD ALIGN=CENTER BGCOLOR="#800000">
<FONT STYLE="ARIAL NARROW" COLOR="#ffffff" SIZE=1>
Title</FONT>
</TD>
</TR>
<!-- Get Data -->
<% do while not RStitleList.EOF %>
<TR>
<TD BGcolor ="f7efde" align=center><font style ="arial narrow" size=1>
<%=RStitleList("title_id")%></font>
</TD>
<TD BGcolor ="f7efde" align=center><font style ="arial narrow" size=1>
<%=RSTitleList("title") %> </font>
</TD>
</TR>
<% RSTitleList.MoveNext%>
<%loop %>
<!-- Next Row -->
</TABLE>
</center>
</BODY>
</HTML>
Like any automation DLLs, a Visual C++ application can use the CoCreateInstance function to get pointers for ADO automation interfaces. The ADO globally unique identifiers (GUIDs) are defined in the Adoid.h file, and the ADO interfaces header is defined in the adoint.h file.
Visual C++ version 5.0 introduced the #import directive, a new mechanism for manipulating OLE automation DLLs. Visual C++ version 5.0 applications can use the #import directive as an alternative for instantiating ADO interface pointers, instead of including Adoid.h and Adoint.h files.
The example shows using the ADO Connection object to establish a connection in Visual C++:
#include <adoid.h>
#include <adoint.h>
HRESULT Connect(char *m_strSource, char* m_strUser, char* m_strPassword, ADOConnection** piConnection)
{
BSTR bstrSource = NULL;
BSTR bstrUser = NULL;
BSTR bstrPassword = NULL;
HRESULT hr;
WCHAR wszBuff[512]; // String Buffer (WCHAR)
ADOConnection* piTmpConnection;
// Open the database.
*piConnection = NULL;
hr = CoInitialize(NULL);
if (FAILED(hr)) goto err_exit;
// Gets an interface pointer for the ADO connection object.
hr = CoCreateInstance(CLSID_CADOConnection,
NULL, CLSCTX_INPROC_SERVER, IID_IADOConnection, (LPVOID *)&piTmpConnection);
if (FAILED(hr)) return hr;
MultiByteToWideChar(CP_ACP, 0, m_strSource, -1, wszBuff, 512);
bstrSource = SysAllocString(wszBuff);
MultiByteToWideChar(CP_ACP, 0, m_strUser, -1, wszBuff, 512);
bstrUser = SysAllocString(wszBuff);
MultiByteToWideChar(CP_ACP, 0, m_strPassword, -1, wszBuff, 512);
bstrPassword = SysAllocString(wszBuff);
// Establish a connection.
hr = piTmpConnection->Open( bstrSource, bstrUser, bstrPassword );
if (FAILED(hr)) goto err_exit;
*piConnection = piTmpConnection;
err_exit:
SysFreeString(bstrSource);
SysFreeString(bstrUser);
SysFreeString(bstrPassword);
return hr;
}
Java applications can be used to import to the ADODB classes and then declare variables to associate with the ADO objects or to create the ADO objects with the new operators.
The example shows using the ADO Connection object to establish a connection in Java:
import msado10.*;
_Connection m_conn = null;
_Recordset m_rs = null;
_Command m_cmd = null;
void OpenConnection()
{
String s;
Properties properties;
try
{ properties = m_conn.getProperties();
m_conn.Open("dsn=pubs", "sa", "");
properties = null;
}
catch (Exception e)
{ System.out.println("\nUnable to make a connection \n");
}
}