Article ID: 176936 - Last Review: March 2, 2005 - Revision: 4.4 INFO: Visual Basic Accessing an Oracle Database Using ADOThis article was previously published under Q176936
For a Microsoft Visual Basic .NET version of this article, see 308071
(http://support.microsoft.com/kb/308071/EN-US/
)
.
SUMMARY
With Visual Basic and ADO, you have the ability to connect to an
Oracle database through a DSN-Less connection, execute a stored procedure
using parameters, and get return values from that stored procedure. The
example in this article illustrates all of this functionality.
MORE INFORMATION
To run the sample code in this article, you may need to download and
install the Microsoft Data Access Components if you are using Visual Basic 5.0.
The MDAC Components are located at:
http://msdn.microsoft.com/en-us/data/aa937729.aspx
(http://msdn.microsoft.com/en-us/data/aa937729.aspx)
The following example was created against an Oracle 7.3 database through a
SQL*Net 2.3 connection. All of the following code (including the stored
procedure) should work fine with Oracle 7.2. However, the Microsoft ODBC
Driver for Oracle Help file states that it only supports SQL*Net 2.3.
There are two objects that need to be created on the Oracle database; a table (adooracle) and a stored procedure (adoinsert). NOTE: If you have worked through the following Microsoft Knowledge Base article then you can use the Oracle objects created in that article (rdooracle and rdoinsert). Just change the Visual Basic code below accordingly: Here are the data definition language (DDL) scripts to create these objects: ADOORACLE - This is just a two-column table with the first column set as the primary key: ADOINSERT - This procedure accepts a single numeric input parameter and returns a single numeric output parameter. The input parameter is first used by an input statement, then it is divided by 2 and set as the output parameter: In SQL 3.3, use a foward slash (/) to terminate and execute the script declaring the stored procedure. NOTE: You must use Procedures that have output parameters and not Functions when working with Oracle and ADO parameters. The preceding scripts can be run from SQL*Plus. Once these objects have been created, you can create the Visual Basic project that will use them. This sample project uses a simple form to send a bind parameter to the ADOINSERT stored procedure and then return the output parameter from that procedure. Here are the steps to create the project:
What follows is a detailed explanation of the code used in this demonstration project. The Form_Load event contains the code that creates the DSN-Less connection: The connect string that is used to open a connection to an Oracle database (or any database for that matter) is very dependant on the underlying ODBC driver. You can see in the connect string below that the Microsoft Oracle driver you are using is named specifically by DRIVER=: Also in the Form_Load event is the code that creates the two ADO Command objects used in the project:
"Use the CommandType property to optimize evaluation of the CommandText
property. If the CommandType property value equals adCmdUnknown (the
default value), you may experience diminished performance because ADO must
make calls to the provider to determine if the CommandText property is an
SQL statement, a stored procedure, or a table name. If you know what type
of command you're using, setting the CommandType property instructs ADO to
go directly to the relevant code. If the CommandType property does not
match the type of command in the CommandText property, an error occurs when
you call the Execute method."
Using the WITH command, you can create and append parameters to the command
object easily. The first parameter of the CreateParameter function is for
the name of the parameter. This has been left blank because the sample
program uses the index of the parameters collection to identify the
individual parameters (such as CPw1(0) to identify the first parameter).
The sample program uses adInteger and adDouble datatypes. If it had used a
variable length datatype, then the size parameter of the CreateParameter
function would need to be set. Again, from the ADO Help HTML:
"If you specify a variable-length data type in the Type argument, you must
either pass a Size argument or set the Size property of the Parameter
object before appending it to the Parameters collection; otherwise, an
error occurs."
The remainder of the project is fairly straightforward and well-documented
in both the Online Help file and Books Online which come with Visual Basic.
The ADO issues that are critical to working with Oracle (the connect
string and the calling of stored procedures) have been detailed in this
project.
REFERENCES
For more information on these issues, please consult your Oracle SQL*Net
2.3 documentation, the Help file for the Microsoft ODBC Driver for Oracle,
the ADO HTML that comes with MDAC, Books Online that comes with Visual Basic 6.0 or your Oracle 7 server documentation. Microsoft ODBC Driver for Oracle Help File ADO Help HTML "Oracle PL/SQL Programming" by Steven Feuerstein "Hitchhiker's Guide to Visual Basic & SQL Server" by William Vaughn, Fifth Edition For additional information, please see the following articles in the Microsoft Knowledge Base: 174679
(http://support.microsoft.com/kb/174679/EN-US/
)
HOWTO: Retrieve Resultsets from Oracle Stored Procedures 175018 (http://support.microsoft.com/kb/175018/EN-US/ ) HOWTO: Acquire and Install the Microsoft Oracle ODBC Driver 174981 (http://support.microsoft.com/kb/174981/EN-US/ ) HOWTO: Retrieve Typical Resultsets from Oracle Stored Procedures 167225 (http://support.microsoft.com/kb/167225/EN-US/ ) HOWTO: Access an Oracle Database Using RDO 176086 (http://support.microsoft.com/kb/176086/EN-US/ ) HOWTO: Retrieve Recordsets from Oracle Stored Procs Using ADO APPLIES TO
| Article Translations
|
Back to the top
