HOWTO: Access an Oracle Database Using RDOLast reviewed: September 5, 1997Article ID: Q167225 |
The information in this article applies to:
SUMMARYWith Visual Basic 5.0 Enterprise edition, 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 INFORMATIONThe example in this article is a simple Visual Basic 5.0 project that creates a DSN-Less connection against an Oracle database and executes a stored procedure. When working with Oracle and the Microsoft ODBC Driver for Oracle, there are some settings that are different than working with Microsoft SQL Server. These differences occur because you are using a different ODBC driver. The Visual Basic 5.0 Enterprise edition includes RDO 2.0 and the Microsoft ODBC Driver for Oracle. Both of these need to be installed in order to run the example project in this article. NOTE: The ODBC driver from Oracle does not support the entire RDO 2.0 feature set (including the calling of stored procedures and binding return values). 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. But, 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 (rdooracle) and a stored procedure (rdoinsert). Following are the data definition language (DDL) scripts to create these objects:
The above scripts can be run from SQL*Plus or from within the Visual Database Tools Add-In in the Visual Basic 5.0 Enterprise edition. Once these objects have been created, you can create the Visual Basic project that will use them.
Step-by-Step ExampleThis example project uses a simple form to send a bind parameter to the RDOINSERT stored procedure and then return the output parameter from that procedure. Here are the steps to create the project:
Following 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. It also contains the code that creates the query that calls the stored procedure:
Private Sub Form_Load() Conn = "UID=;PWD=;driver={Microsoft ODBC Driver for Oracle};" _ & "CONNECTSTRING=MyOracle;" Set En = rdoEnvironments(0) Set Cn = En.OpenConnection("", rdDriverPrompt, False, Conn) QSQL = "{call rdoinsert(?,?)}" Set CPw = Cn.CreateQuery("", QSQL) End SubNote that you are not using the rdPreparedStatement object. This object has been replaced by the rdoQuery object. This is new for RDO 2.0. Also, with RDO 2.0, you do not need to explicitly create a connection object as is done in this project. You can create a stand-alone query object that is not specifically associated with a connection. To learn more about this functionality, look up the rdoQuery Object in the Visual Basic 5.0 Enterprise edition Help file. The connect string used to open a connection to an Oracle database (or any database) is very dependent upon the underlying ODBC driver. Even though similar parameters are used by most ODBC drivers (UID, PWD, etc.), the connect string used here will work only with the Microsoft ODBC Driver for Oracle:
Conn = "UID=;PWD=;driver={Microsoft ODBC Driver for Oracle};" _ & "CONNECTSTRING=MyOracle;"The most important part of this connect string is the "CONNECTSTRING"’ keyword. It is used only by the Microsoft ODBC Driver for Oracle. For Microsoft SQL Server 6.5, you use the keyword "SERVER." The string assigned to CONNECTSTRING is the Database Alias that you set up in SQL*Net. This is the only difference in the connect string when connecting to an Oracle database. All of the other parameters operate as described in the Help file (under rdoConnection Object) for Visual Basic 5.0 Enterprise edition. As stated in the Help file, for a connection, you do not specify a DSN in the connect string. Also in the Form_Load event is the code that creates the query object that calls the stored procedure:
QSQL = "{call rdoinsert(?,?)}" Set CPw = Cn.CreateQuery("", QSQL)With Oracle, you cannot specify a return value for a stored procedure call as you can with Microsoft SQL Server 6.5; you must use stored procedures that have output parameters as noted earlier in this article. The parameter placeholders in the QSQL string are denoted by a "?" and referenced in the order they in which they appear in the string. For more information on the use of parameter placeholders in the rdoQuery object, refer to the rdoParameter object in the Visual Basic 5.0 Enterprise edition Help file. The remainder of the project is fairly straight-forward and well-documented in both the online Help file and Books Online, both of which come with Visual Basic 5.0. The RDO issues critical to working with Oracle, the connect string, and the calling of stored procedures have been detailed in this project. 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, and your Oracle 7 server documentation.
REFERENCESVisual Basic 5.0 Enterprise Edition Help files Oracle SQL*Net 2.3 Help file Hitchhiker’s Guide to Visual Basic & SQL Server - Fourth Edition.
|
Additional query words: Oracle RDO Stored Procedure
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |