Article ID: 176936
Article Last Modified on 3/2/2005
167225 HOWTO: Access an Oracle Database Using RDO
CREATE TABLE adooracle (
item_number NUMBER(3) PRIMARY KEY,
depot_number NUMBER(3));
CREATE OR REPLACE PROCEDURE adoinsert (
insnum IN NUMBER, outnum OUT NUMBER)
IS
BEGIN
INSERT INTO adooracle
(Item_Number, Depot_Number)
VALUES
(insnum, 16);
outnum := insnum/2;
END;
/
Control Name Text/Caption Button cmdCheck Check Button cmdSend Send Text Box txtInput Label lblInput Input:
Option Explicit
Dim Cn As ADODB.Connection
Dim CPw1 As ADODB.Command
Dim CPw2 As ADODB.Command
Dim Rs As ADODB.Recordset
Dim Conn As String
Dim QSQL As String
Private Sub cmdCheck_Click()
CPw1(0) = Val(txtInput.Text)
Set Rs = CPw1.Execute
MsgBox "Item_Number = " & Rs(0) & ". Depot_Number = " & Rs(1) & "."
Rs.Close
End Sub
Private Sub cmdSend_Click()
CPw2(0) = Val(txtInput.Text)
CPw2.Execute
MsgBox "Return value from stored procedure is " & CPw2(1) & "."
End Sub
Private Sub Form_Load()
'You will need to replace the "*" with the appropriate values.
Conn = "UID=*****;PWD=****;DRIVER={Microsoft ODBC for Oracle};" _
& "SERVER=*****;"
Set Cn = New ADODB.Connection
With Cn
.ConnectionString = Conn
.CursorLocation = adUseClient
.Open
End With
QSQL = "Select Item_Number, Depot_Number From adooracle Where " _
& "item_number = ?"
Set CPw1 = New ADODB.Command
With CPw1
.ActiveConnection = Cn
.CommandText = QSQL
.CommandType = adCmdText
.Parameters.Append .CreateParameter(, adInteger, adParamInput)
End With
QSQL = "adoinsert"
Set CPw2 = New ADODB.Command
With CPw2
.ActiveConnection = Cn
.CommandText = QSQL
.CommandType = adCmdStoredProc
.Parameters.Append .CreateParameter(, adInteger, adParamInput)
.Parameters.Append .CreateParameter(, adDouble, adParamOutput)
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
Cn.Close
Set Cn = Nothing
Set CPw1 = Nothing
Set CPw2 = Nothing
End Sub
Conn = "UID=<uid>;PWD=<pwd>;DRIVER={Microsoft ODBC for Oracle};" _
& "SERVER=<MyServer>;"
Set Cn = New ADODB.Connection
With Cn
.ConnectionString = Conn
.CursorLocation = adUseClient
.Open
End With
Once you create the ADO connection object (Cn), you set several of its
parameters using the WITH statement.
Conn = "UID=<uid>;PWD=<pwd>;DRIVER={Microsoft ODBC for Oracle};" _
& "SERVER==<MyServer>;"
The most important part of this connect string is the "SERVER" keyword. The
string assigned to SERVER is the Database Alias which you set up in
SQL*Net. This is the only difference in the connect string when connecting
to an Oracle database. For a DSN-Less connection, as is stated in the Help
file, you do not specify a DSN in the connect string.
QSQL = "Select Item_Number, Depot_Number From adooracle Where " _
& "item_number = ?"
Set CPw1 = New ADODB.Command
With CPw1
.ActiveConnection = Cn
.CommandText = QSQL
.CommandType = adCmdText
.Parameters.Append .CreateParameter(, adInteger, adParamInput)
End With
QSQL = "adoinsert"
Set CPw2 = New ADODB.Command
With CPw2
.ActiveConnection = Cn
.CommandText = QSQL
.CommandType = adCmdStoredProc
.Parameters.Append .CreateParameter(, adInteger, adParamInput)
.Parameters.Append .CreateParameter(, adDouble, adParamOutput)
End With
The first Command object (CPw1) is a simple parameterized query. The
CommandText has one parameter that is the item_number for the where clause.
Note that the CommandType is set to adCmdText. This is different than the
adCmdStoredProc CommandType in the second Command object (CPw2). The following is from the ADO Help HTML file:
"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."
"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."
174679 HOWTO: Retrieve Resultsets from Oracle Stored Procedures
175018 HOWTO: Acquire and Install the Microsoft Oracle ODBC Driver
174981 HOWTO: Retrieve Typical Resultsets from Oracle Stored Procedures
167225 HOWTO: Access an Oracle Database Using RDO
176086 HOWTO: Retrieve Recordsets from Oracle Stored Procs Using ADO
Additional query words: oracle stored procedures ado
Keywords: kbinfo kboracle kbstoredproc kbdatabase KB176936