Luis Neri was kind enough to translate my "Getting Started with OCCI (Windows Version)" post into Spanish. Below is his translation of the original post. Thanks very much to Luis for this. However, please note that I do not speak Spanish, so I won't be able to respond to any comments in that language. - Mark La Oracle C++ Call Interface, también conocida como OCCI, es una API construida sobre otras API's de bajo nivel de Oracle. Uno de los objetivos de OCCI es ofrecer a los programadores de C++ una forma de acceso fácil a las bases de datos de Oracle en una forma similar a la que tienen los programadores de Java con "Java Database Connectivity (JDBC) ". Este documento trata de dar una vista rápida para empezar con esta tecnología, la cual puede ser incorporada a los desarrollos y aplicaciones GIS del SIRAN; si se desea, existe más información en la documentación en línea de Oracle. http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28390/toc.htm Este "Empezando con OCCI" tiene el objeto de dar un método para la puesta del ambiente el cual usa OCCI en los desarrollos con C++ bajo Windows para acceder a las bases de datos de Oracle. El ambiente El ambiente en que se realizarón las pruebas para la puesta en funcionamiento de esta tecnología es el que abajo se detalla, tenga en cuenta que a este ambiente se pueden hacer pequeñas adaptaciones para que funcione en el suyo en particular. Información importante Uno de las características más importantes al trabajar con OCCI es que se debe asegurar que todos los componentes del ambiente de desarrollo y de runtime están soportados en las combinaciones y tener las versiones correctas. Se hace un fuerte énfasis en esto, si usted no observa con atención esta recomendación seguramente encontrara problemas. Para lograr esto por favor encuentre las combinaciones correctas de las versiones en la pagina de Oracle Technology Network (OTN). Descargue los componentes correctos Al momento de escribir este documento se encontraron para el ambiente descrito arriba los siguientes componentes Estos componentes se deben descargar en la máquina de desarrollo. Yo lo hice en el escritorio. Instalación de Instant Client La instalación es tan simple como descomprimir el archivo, descomprímalo sobre C:\ , el resultado será una carpeta C:\instantclient_11_1 con las subcarpetas "sdk", "vc8" y "vc7", las cuales no serán utilizadas en el desarrollo sobre nuestro ambiente. Instalación del OCCI Package Al igual que la instalación de Instant Client, los paquetes Occi pueden ser descomprimidos, pero sin embargo en vez de crear un directorio sobre c:\, los descomprimí sobre el escritorio, una vez descomprimidos, me desvié un poco de lo quel archivo occivc9_111060_readme.txt dice, y realice los siguiente: Cree un directorio "vc9" dentro del directorio "sdk" como sigue: C:\instantclient_11_1\sdk\lib\msvc\vc9 Cree una carpeta "vc9" dentro de "instantclient_11_1" como sigue: C:\instantclient_11_1\vc9 Borre el archivo oraocci11.dll y oraocci11.sym del directorio C:\instantclient_11_1. Estos archives no están hechos para construir o compilar con Visual Studio 2008 y como se dijo anteriormente es importante que se empaten las versiones. Se extrajeron los archivos OCCI en la carpeta del escritorio moviendo los dos archivos a la carpeta C:\instantclient_11_1\sdk\lib\msvc\vc9 antes creada: de la misma carpeta en el escritorio, se mueven los siguientes archivos a la carpeta C:\instantclient_11_1\vc9 antes creada: Finalmente se borra el archivo oraocci11.lib de: C:\instantclient_11_1\sdk\lib\msvc Otra vez este archivo no es compatible con nuestro ambiente. Despues de realizar estos pasos, los archivos .lib deben estar bajo la carpeta C:\instantclient_11_1\sdk\lib\msvc y los archivos .dll y .manifest deben estar bajo la carpeta C:\instantclient_11_1. Esto pasos parecen trabajo extra, pero resultan de una separación completa de las varias versiones OCCI y lo hace más fácil y explicito. Para especificar que versión de librerías OCCI usar, añada estas carpetas al path del sistema. Estas dos carpetas deben estar al inicio del path: C:\instantclient_11_1\vc9;C:\instantclient_11_1;{y lo demás del path…} Configuración de Visual Studio El ambiente Windows ha sido configurado para el uso de los nuevos paquetes OCCI y Instan Client (los adecuados para nuestro ambiente), pero antes de empezar un desarrollo en Visual Studio, es necesario establecer algunas opciones. Sin estas opciones Visual Studio es incapaz de encontrar los archivos correctos y construir aplicaciones. Hay dos opciones las cuales necesitan ser especificadas: UsandoVisual C++ 2008, las rutas de menu donde se especifican estas opciones son: Cree un proyecto simple de prueba Ya que se ha hecho todo la puesta en funcionamiento el ambiente está configurado!, Utilice la siguiente proyecto como prueba para verificar que todo funciona como se espera. Otra vez, este es un simple ejemplo para verificar que las cosas funcionan correctamente. No es un templeate de desarrollo. Cree en Visual C++ 2008 un proyecto seleccionando File –> New –> Project… del menú principal, seleccione "Win32" como tipo de proyecto, seleccione "Win32 Console Application", dele un nombre al proyecto (yo usé prueba_occi), seleccione un carpeta donde guardar deseleccione "Create directory for solution", y presione OK. Presione Next en el Wizard, deseleccione Precompiled header, presione Empty project, y presione Finish. En el explorador de la solucion, click-izquierdo en Header Files, seleccione Add, seleccione New Item… En Add New Item, seleccione Header File (.h), introduzca Employees.h (o cualquier otro nombre) en nombre y presione Add. /* #include <occi.h> using namespace oracle::occi; class Employees { void List(); private: string user; en Solution Explorer, click-izquierdo en Source Files, seleccione Add, seleccione New Item… en Add New Item, seleccione C++ File (.cpp), introduzca Employees.cpp ((o cualquier otro nombre) en nombre y presione Add. Este es el contenido de mi archivo en el sistema: /* #include "Employees.h" using namespace std; int main (void) Employees *pEmployees = new Employees(); pEmployees->List(); delete pEmployees; cout << "ENTER to continue..."; cin.get(); return 0; Employees::Employees() user = "hr"; env = Environment::createEnvironment(Environment::DEFAULT); try exit(EXIT_FAILURE); Employees::~Employees() Environment::terminateEnvironment (env); void Employees::List() Statement *stmt = NULL; try if (stmt) rs = stmt->executeQuery(); if (rs) while (rs->next()) { cout << endl; stmt->closeResultSet(rs); con->terminateStatement(stmt); Antes de construir el ejemplo (build), se necesita añadir la librería OCCI a la lista de entradas del linker: Seleccione Project –> prueba_occi Properties... del menu (Sustituya el nombre por el proio si es necesario) Expanda el nodo Configuration Properties, expanda el nodo Linker, seleccione Input item, introduzca "oraocci11d.lib" para debug build o "oraocci11.lib" para release build. Seleccione Build –> Build Solution del menú para construir la solución. Si todo está puesto correctamente no debería haber errores. Si existen error busque donde pueden estar y corrija. La pantalla de ejecución seria como esta: ID FIRST NAME LAST NAME [ snip ] 120 Matthew Weiss ENTER to continue... Si eres nuevo en el uso de OCCI sobre Windows con Visual Studio 2008, quizá el ejemplo de arriba puede ser de ayuda al inicio.
* A simple OCCI test application
* This file contains the Employees class declaration
*/
#include <iostream>
#include <iomanip>
using namespace std;
public:
Employees();
virtual ~Employees();
Environment *env;
Connection *con;
string passwd;
string db;
};
* A simple OCCI test application
* This file contains the Employees class implementation
*/
using namespace oracle::occi;
{
/*
* create an instance of the Employees class,
* invoke the List member, delete the instance,
* and prompt to continue...
*/
}
{
/*
* connect to the test database as the HR
* sample user and use the EZCONNECT method
* of specifying the connect string. Be sure
* to adjust for your environment! The format
* of the string is host:port/service_name
*/
passwd = "hr";
db = "oel01:1521/OEL11GR1.SAND";
{
con = env->createConnection(user, passwd, db);
}
catch (SQLException& ex)
{
cout << ex.getMessage();
}
}
{
env->terminateConnection (con);
}
{
/*
* simple test method to select data from
* the employees table and display the results
*/
ResultSet *rs = NULL;
string sql = "select employee_id, first_name, last_name " \
"from employees order by last_name, first_name";
{
stmt = con->createStatement(sql);
}
catch (SQLException& ex)
{
cout << ex.getMessage();
}
{
try
{
stmt->setPrefetchRowCount(32);
}
catch (SQLException& ex)
{
cout << ex.getMessage();
}
{
cout << endl << setw(8) << left << "ID"
<< setw(22) << left << "FIRST NAME"
<< setw(27) << left << "LAST NAME"
<< endl;
cout << setw(8) << left << "======"
<< setw(22) << left << "===================="
<< setw(27) << left << "========================="
<< endl;
cout << setw(8) << left << rs->getString(1)
<< setw(22) << left << (rs->isNull(2) ? "n/a" : rs->getString(2))
<< setw(27) << left << rs->getString(3)
<< endl;
}
}
}
}
====== ==================== =========================
174 Ellen Abel
166 Sundar Ande
130 Mozhe Atkinson
105 David Austin
204 Hermann Baer
116 Shelli Baida
167 Amit Banda
172 Elizabeth Bates
200 Jennifer Whalen
149 Eleni Zlotkey
28 July 2009
Empezando con OCCI
Posted by
Mark Williams
at
22:43
11 July 2009
SQL*Plus, EZCONNECT, Password Prompt, and ORA-12504
I use SQL*Plus a great deal and I find the "Easy Connect Naming Method" a convenient option.
Before you get any further in this post, let me be perfectly clear that this post only addresses ORA-12504 in the context of using the "Easy Connect Naming Method" with SQL*Plus and password prompting. It does not address ORA-12504 outside of this context. The documentation (see the link above) has several examples of using and configuring this method, so I won't go into them all here. This method is also known as "EZCONNECT" presumably because "Z" in the American dialect is pronounced like "zee" rather than "zed".
You are likely already familiar with the password prompting behavior of SQL*Plus. For example, when using a tnsnames.ora file alias to connect to a database, if the password is not specified, SQL*Plus will prompt you for it (i.e. "Enter password:"):
[oracle@liverpool ~]$ sqlplus hr@orademo
SQL*Plus: Release 11.1.0.7.0 - Production on Sat Jul 11 11:42:08 2009
Copyright (c) 1982, 2008, Oracle. All rights reserved.
Enter password:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
Nice and easy.
So, let's try the equivalent using EZCONNECT:
[oracle@liverpool ~]$ sqlplus hr@liverpool:1521/DEMO
SQL*Plus: Release 11.1.0.7.0 - Production on Sat Jul 11 11:43:20 2009
Copyright (c) 1982, 2008, Oracle. All rights reserved.
ERROR:
ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA
Enter user-name:
Hmm... well, that's interesting. Rather than getting prompted for the password we get an error (ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA) and then we get presented with the "Enter user-name:" prompt again. Funny that the error is saying the SERVICE_NAME was not specified, but it sure is there - DEMO in this case. This is giving us a clue though...
OK, so let's go ahead and specify the password:
[oracle@liverpool ~]$ sqlplus hr/hr@liverpool:1521/DEMO
SQL*Plus: Release 11.1.0.7.0 - Production on Sat Jul 11 11:44:02 2009
Copyright (c) 1982, 2008, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
Much better, it works, but what if you (reasonably) don't want to specify the password on the command-line?
Taking a look at the documentation (really - that stuff does come in handy!) for the CONNECT SQL*Plus command we can see this:
username[/password] [@connect_identifier]
Pay particular attention that the "/" precedes the password. In the case of using EZCONNECT we have:
sqlplus hr@liverpool:1521/DEMO
So, in this case the "/" precedes the SERVICE_NAME and not the password. How should we tell SQL*Plus that the "/" is not preceding the password? We quote the connection string!
[oracle@liverpool ~]$ sqlplus hr@\"liverpool:1521/DEMO\"
SQL*Plus: Release 11.1.0.7.0 - Production on Sat Jul 11 11:45:43 2009
Copyright (c) 1982, 2008, Oracle. All rights reserved.
Enter password:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
Now that's more like it! Notice that I used "\" (backslash) to escape the double-quote characters on the command-line (I'm using the bash shell for this).
If you are already in SQL*Plus you do not need to use the backslash to escape the double-quote characters:
[oracle@liverpool ~]$ sqlplus /nolog
SQL*Plus: Release 11.1.0.7.0 - Production on Sat Jul 11 11:46:31 2009
Copyright (c) 1982, 2008, Oracle. All rights reserved.
SQL> connect hr@"liverpool:1521/DEMO"
Enter password:
Connected.
SQL>
This same technique can also be used for privileged connections:
[oracle@liverpool ~]$ sqlplus sys@\"liverpool:1521/DEMO\" as sysdba
SQL*Plus: Release 11.1.0.7.0 - Production on Sat Jul 11 11:47:23 2009
Copyright (c) 1982, 2008, Oracle. All rights reserved.
Enter password:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
If you use SQL*Plus as much as I do, perhaps this will be a handy tip.
Posted by
Mark Williams
at
15:18
07 July 2009
Getting Started with OCCI (Windows Version)
The Oracle C++ Call Interface, also known as OCCI, is an application programming interface (API) built upon the Oracle Call Interface (OCI - another lower level API from Oracle). One of the goals of OCCI is to offer C++ programmers easy access to Oracle Database in a fashion similar to what Java Database Connectivity (JDBC) affords Java developers. If you would like to learn more about what OCCI is (and isn't), pay a visit to the OCCI documentation on Oracle Technology Network (OTN) here:
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28390/toc.htm
My goal with this "Getting Started" post is to give you one method of setting up an environment in which you can use OCCI to develop C++ applications under Windows that access Oracle Database. I am not in any way covering all possible scenarios or delving deep into OCCI itself. Please note that the database itself can be on any supported host.
The Environment
Your environment is likely to differ from mine; however, it is important to be familiar with the various components in the environment used here so that you can make adaptations as necessary for your specific environment.
- Oracle Database Server/Host: oel01 (Oracle Enterprise Linux 32-bit server)
- Oracle Database: SID value of OEL11GR1, Service Name value of OEL11GR1.SAND, version 11.1.0.7
- Development Machine: Hostname of CHEPSTOW, Windows XP Professional 32-bit
- Development IDE: Microsoft Visual C++ 2008 Express Edition (Windows SDK also installed)
- Oracle Client: Oracle Instant Client with OCCI
Important Information
One of the most crucial attributes of working with OCCI is that you must ensure that all of the components of the development environment and the runtime environment are supported combinations and correct versions. I can not emphasise this enough. If you deviate from this, you will almost certainly find trouble! In order to find the correct combinations of products and versions, see the following links on OTN:
- Oracle C++ Call Interface
- OCCI Downloads page
- Oracle Database Instant Client
- Instant Client Downloads
- Instant Client Downloads for Windows 32-bit
Download the Correct Packages
At the time of this writing, the following are the component versions supported for the environment listed above:
- OCCI 11.1.0.6 (Visual C++9 (VS 2008)[Windows 32-bit])
- Instant Client Version 11.1.0.6
From the download links above, you should download the following components to your development machine. I downloaded them to the C:\Temp directory.
- OCCI 11.1.0.6 (Visual C++9 (VS 2008)[Windows 32-bit]) - occivc9win32_111060.zip
- Instant Client Package - Basic: instantclient-basic-win32-11.1.0.6.0.zip
- Instant Client Package - SDK: instantclient-sdk-win32-11.1.0.6.0.zip
- Instant Client Package - SQL*Plus: instantclient-sqlplus-win32-11.1.0.6.0.zip (optional, but I always install it)
Install the Instant Client Packages
Installing the Instant Client packages is simply a matter of unzipping them – not much to wrong here! I unzipped them all to the C:\ directory on Chepstow. This resulted in a new top-level directory - C:\instantclient_11_1 with "sdk", "vc8", and "vc7" directories underneath. The "vc8" and "vc7" directories should be ignored in the context of the environment created here.
Install the OCCI Package
Much like the Instant Client packages, the OCCI package should be unzipped; however, rather than unzipping it to the C:\ directory, I unzipped it to the C:\Temp directory. Once unzipped, review the occivc9_111060_readme.txt file for information; however, I deviate from the directories listed in the file.
I create a "vc9" directory under the "sdk" directory as follows:
C:\instantclient_11_1\sdk\lib\msvc\vc9
I create a "vc9" directory under the "instantclient_11_1" directory as follows:
C:\instantclient_11_1\vc9
I delete the oraocci11.dll and oraocci11.sym files from the C:\instantclient_11_1 directory. These files are not built/linked with the runtime libraries used by Visual Studio 2008 and, as mentioned above, it is critical that component versions match!
From the extracted OCCI files in the C:\Temp directory, move the following two files to the C:\instantclient_11_1\sdk\lib\msvc\vc9 directory previously created:
- oraocci11.lib
- oraocci11d.lib
From the extracted OCCI files in the C:\Temp directory, move the following four files to the C:\instantclient_11_1\vc9 directory previously created:
- oraocci11.dll
- oraocci11.dll.manifest
- oraocci11d.dll
- oraocci11d.dll.manifest
Finally, delete the oraocci11.lib file from the C:\instantclient_11_1\sdk\lib\msvc directory. Again, this file is not compatible with the environment created here.
After performing these steps, the .lib files should only be in directories under C:\instantclient_11_1\sdk\lib\msvc and the .dll and .manifest files should only be in directories under the C:\instantclient_11_1 directory. While this may seem like extra unneeded work, it results in complete separation of the various versions of the OCCI components making it easier (and explicit) which version is to be used.
To specify which version of the OCCI libraries are used, add the directory to the system path. You also add the the Instant Client directory to the path. Both of these directories should be added to the beginning of the system path:
C:\instantclient_11_1\vc9;C:\instantclient_11_1;{rest of path follows…}
Configure Visual Studio
The Windows environment has been configured to use the new OCCI and Instant Client packages but before you can begin developing in Visual Studio, you need to set a few options. Without these options Visual Studio will be unable to find the correct files and build your applications. There are two options that need to be specified:
- Include files – allows Visual Studio to find the header files for OCCI
- Library files – allows Visual Studio to find the library files for OCCI
Using Visual C++ 2008 Express Edition, the menu paths to specify these options are as follows:
- Tools –> Options… Expand "Projects and Solutions" node, select "VC++ Directories", under "Show directories for:" select "Include files", double-click under the last entry to open a new box to enter a path, enter "C:\instantclient_11_1\sdk\include" and press enter
- Under "Show directories for:" select "Library files", double-click under the last entry to open a new box to enter a path, enter "C:\instantclient_11_1\sdk\lib\msvc\vc9" and press enter
- Click OK to save the settings
Create a Simple Test Project
All the setup work is now complete and the environment is configured! If needed, you can use the following (very!) basic application as a simple test to verify things are working as expected. Again, this is a simple example only to verify things are setup correctly. It is not intended to be a complete template for "proper" code development, etc.
Create the Visual C++ 2008 Express Edition project by selecting File –> New –> Project… from the main menu, select "Win32" as the project type, select "Win32 Console Application", give the project a name (I used OCCITest), select a location (I used C:\Projects), I unchecked "Create directory for solution", and then click OK.
Click Next in the Application Wizard, uncheck Precompiled header, click Empty project, and click Finish.
In Solution Explorer, right-click Header Files, select Add, select New Item…
In Add New Item, select Header File (.h), enter Employees.h (or any name you prefer) in Name, and click Add.
Here's the content of the file on my system:
/*
* A simple OCCI test application
* This file contains the Employees class declaration
*/
#include <occi.h>
#include <iostream>
#include <iomanip>
using namespace oracle::occi;
using namespace std;
class Employees {
public:
Employees();
virtual ~Employees();
void List();
private:
Environment *env;
Connection *con;
string user;
string passwd;
string db;
};
In Solution Explorer, right-click Source Files, select Add, select New Item…
In Add New Item, select C++ File (.cpp), enter Employees.cpp (or any name you prefer) in Name, and click Add.
Here's the content of the file on my system:
/*
* A simple OCCI test application
* This file contains the Employees class implementation
*/
#include "Employees.h"
using namespace std;
using namespace oracle::occi;
int main (void)
{
/*
* create an instance of the Employees class,
* invoke the List member, delete the instance,
* and prompt to continue...
*/
Employees *pEmployees = new Employees();
pEmployees->List();
delete pEmployees;
cout << "ENTER to continue...";
cin.get();
return 0;
}
Employees::Employees()
{
/*
* connect to the test database as the HR
* sample user and use the EZCONNECT method
* of specifying the connect string. Be sure
* to adjust for your environment! The format
* of the string is host:port/service_name
*/
user = "hr";
passwd = "hr";
db = "oel01:1521/OEL11GR1.SAND";
env = Environment::createEnvironment(Environment::DEFAULT);
try
{
con = env->createConnection(user, passwd, db);
}
catch (SQLException& ex)
{
cout << ex.getMessage();
exit(EXIT_FAILURE);
}
}
Employees::~Employees()
{
env->terminateConnection (con);
Environment::terminateEnvironment (env);
}
void Employees::List()
{
/*
* simple test method to select data from
* the employees table and display the results
*/
Statement *stmt = NULL;
ResultSet *rs = NULL;
string sql = "select employee_id, first_name, last_name " \
"from employees order by last_name, first_name";
try
{
stmt = con->createStatement(sql);
}
catch (SQLException& ex)
{
cout << ex.getMessage();
}
if (stmt)
{
try
{
stmt->setPrefetchRowCount(32);
rs = stmt->executeQuery();
}
catch (SQLException& ex)
{
cout << ex.getMessage();
}
if (rs)
{
cout << endl << setw(8) << left << "ID"
<< setw(22) << left << "FIRST NAME"
<< setw(27) << left << "LAST NAME"
<< endl;
cout << setw(8) << left << "======"
<< setw(22) << left << "===================="
<< setw(27) << left << "========================="
<< endl;
while (rs->next()) {
cout << setw(8) << left << rs->getString(1)
<< setw(22) << left << (rs->isNull(2) ? "n/a" : rs->getString(2))
<< setw(27) << left << rs->getString(3)
<< endl;
}
cout << endl;
stmt->closeResultSet(rs);
}
con->terminateStatement(stmt);
}
}
Before you can build the sample, you need to add the OCCI library to the input list for the linker:
Select Project –> OCCITest Properties... from the menu (substitute your project name if different)
Expand Configuration Properties node, expand Linker node, select Input item, enter "oraocci11d.lib" for a debug build or "oraocci11.lib" for a release build.
Select Build –> Build Solution from the menu to build the solution. If everything is setup correctly, there should be no errors during the build. If you receive errors, investigate and correct them.
Executing the sample should result in output as follows:
ID FIRST NAME LAST NAME
====== ==================== =========================
174 Ellen Abel
166 Sundar Ande
130 Mozhe Atkinson
105 David Austin
204 Hermann Baer
116 Shelli Baida
167 Amit Banda
172 Elizabeth Bates
[ snip ]
120 Matthew Weiss
200 Jennifer Whalen
149 Eleni Zlotkey
ENTER to continue...
If you are new to using OCCI on Windows with Visual Studio, perhaps the above will be helpful in getting started!
Posted by
Mark Williams
at
03:46
18 June 2009
Microsoft To Deprecate System.Data.OracleClient
I found the following to be an interesting announcement:
System.Data.OracleClient Update
It looks like Microsoft have decided to deprecate System.Data.OracleClient beginning with the .NET 4.0 release.
Of course, I'm more than a little biased when it comes to anything related to Oracle.
For more information and to download ODP.NET, please see the Oracle Data Provider for .NET center on Oracle Technology Network (OTN).
Posted by
Mark Williams
at
22:10
31 May 2009
Oracle Pro*C on Windows with Express Edition Products
NOTE: I have edited the intro text here from the original source as a result of some discussions I've had. These discussions are ongoing so I can't post the results as of yet. (3-June-2009 approximately 5:00 PM).
I thought I would take an introductory look at using the Oracle Pro*C precompiler using Express Edition products. Here are the components I will use for this purpose (links valid at time of posting):
- Oracle Database 10g Express Edition (available here)
- Oracle Enterprise Linux (available here)
- Oracle Instant Client 11.1.0.7 Packages for Microsoft Windows 32-bit (available here)
- Instant Client Package – Basic
- Instant Client Package – SDK
- Instant Client Package – Precompiler
- Instant Client Package - SQL*Plus
- Microsoft Windows XP Professional 32-bit with Service Pack 3
- Microsoft Visual C++ 2008 Express Edition (available here)
- Windows SDK for Windows Server 2008 and .NET Framework 3.5 (available here)
For the purposes of this discussion you will need to have already installed (or have access to) Oracle Database with the HR sample schema. You will also need to have installed Visual C++ 2008 Express Edition and the Windows SDK on the machine you will use as your development machine. For a walkthrough of installing Visual C++ 2008 Express Edition and the Windows SDK, please see this link. Note that even though the SDK seems to be only for Windows Server 2008 (based on the name) it is supported on XP, Vista, and Windows Server.
In my environment I have installed Oracle Database 10g Express Edition on a host running Oracle Enterprise Linux. The host name is "oel02" (not especially clever, I realise). The Windows XP machine that I will use as the development machine is named "chepstow" (perhaps marginally more clever) and Visual C++ Express Edition and the Windows SDK are already installed. I have downloaded the four Instant Client packages listed above to the "c:\temp" directory on chepstow. The SQL*Plus package is not required; however, I find it convenient so I always install it. So, since I already have a database server and the Microsoft tools are installed, all that remains is to install the Instant Client packages.
Installing the Instant Client Packages
It is incredibly easy to install the Instant Client packages – simply unzip them! I chose to unzip them (on chepstow, my development machine) to the "c:\" directory and this created a new "c:\instantclient_11_1" directory and various sub-directories. I then added the following two directories to the system path:
- C:\instantclient_11_1
- C:\instantclient_11_1\sdk
NOTE: I added the two directories to the beginning of the system path and had no other Oracle products installed. See comments for more information about this. (Added 29 June 2009 approximately 11:30 AM)
Setting up the Pro*C Configuration File
I know it is easy to skip reading a README file, but it is important that you do read the PRECOMP_README file in the Instant Client root directory. Pro*C will, by default, look for a configuration file named "pcscfg.cfg" when it is invoked. In the environment that I have created (default installs of all products) Pro*C will want to find this file in the "c:\instantclient_11_1\precomp\admin" directory. However, if you look at your install (if you have done the same as me) you will notice there is no such directory! Therefore you should create this directory ("c:\instantclient_11_1\precomp\admin"). You should then copy the "pcscfg.cfg" file from the "c:\instantclient_11_1\sdk\demo" directory to the "c:\instantclient_11_1\precomp\admin" directory.
The "pcscfg.cfg" file will initially contain the following single line:
define=(WIN32_LEAN_AND_MEAN)
Below this line you add the following four lines:
sys_include=C:\PROGRA~1\MICROS~1.0\VC\include\sys
include=C:\PROGRA~1\MICROS~3\Windows\v6.1\Include
include=C:\PROGRA~1\MICROS~1.0\VC\include
include=C:\instantclient_11_1\sdk\include
Save the file and exit your editor.
Be sure to note that the directory names above are the "short" names to ensure they do not contain spaces. If the directory names contain spaces this will cause problems with the Pro*C precompiler. To help "translate" the directories above, here are the long versions (be sure you do not enter these):
sys_include=C:\Program Files\Microsoft Visual Studio 9.0\VC\include\sys
include=C:\Program Files\Microsoft SDKs\Windows\v6.1\Include
include=C:\Program Files\Microsoft Visual Studio 9.0\VC\include
include=C:\instantclient_11_1\sdk\include
You can find the short names by using "dir /x" in a command-prompt window.
Adding Directories to Visual Studio C++ 2008 Express Edition
Next you should add the Oracle Instant Client include and library directories to Visual Studio. To do this, simply perform the following steps:
- Select Tools –> Options to open the Options dialog
- Expand the "Projects and Solutions" node
- Click the "VC++ Directories" item
- Under "Show directories for:" select "Include files"
- Click underneath the last entry in the list (you should get a highlighted line with no text)
- Click the folder button to create a new line
- Enter "c:\instantclient_11_1\sdk\include" and press Enter
- Under "Show directories for:" select "Library files"
- Click underneath the last entry in the list (you should get a highlighted line with no text)
- Click the folder button to create a new line
- Enter "c:\instantclient_11_1\sdk\lib\msvc" and press Enter
- Click the OK button to save the changes
Create a New Project
WARNING: You should create your project in a directory (and path) that has no spaces in it. If you create the project in a directory or path with spaces you will receive errors during the precompile phase. I used "c:\temp" for this example.
Now create a new project in Visual Studio:
- Select File –> New Project to open the New Project dialog
- Select "Win32" as the project type
- Select "Win32 Console Application" under "Templates"
- Give the project a name (I used "proctest" in keeping with my clever naming tradition)
- I always choose to de-select "Create directory for solution" and click OK
- Click the "Next" button in the application wizard
- Click the "Empty project" checkbox under "Additional options"
- Click the "Finish" button
Create the Pro*C Source File
To create the Pro*C source file, perform the following steps:
- Right-click "Source Files" and select Add –> New Item… from the context menu
- Select "Code" under "Visual C++"
- Select "C++ File (.cpp)" under "Visual Studio installed templates" (note that you will not actually create C++ code in this example)
- Give the file a name such as "proctest.pc" and click "Add"
Here's the Pro*C source I used for this example (this is clearly sample code and lots is left out!):
/*
** suppress certain warnings
*/
#ifdef WIN32
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlca.h>
#include <sqlda.h>
#include <sqlcpr.h>
EXEC SQL BEGIN DECLARE SECTION;
/*
** defines for VARCHAR lengths.
*/
#define UNAME_LEN 30
#define PWD_LEN 30
#define DB_LEN 48
#define FNAME_LEN 32
#define LNAME_LEN 32
/*
** variables for the connection
*/
VARCHAR username[UNAME_LEN];
VARCHAR password[PWD_LEN];
VARCHAR dbname[DB_LEN];
/*
** variables to hold the results
*/
int ctr;
int empid;
VARCHAR fname[FNAME_LEN];
VARCHAR lname[LNAME_LEN];
EXEC SQL END DECLARE SECTION;
/*
** declare error handling function
*/
void sql_error(char *msg)
{
char err_msg[128];
size_t buf_len, msg_len;
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf("\n%s\n", msg);
buf_len = sizeof (err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*s\n", msg_len, err_msg);
EXEC SQL ROLLBACK RELEASE;
exit(EXIT_FAILURE);
}
void main()
{
/*
** Copy the username into the VARCHAR.
*/
strncpy((char *) username.arr, "hr", UNAME_LEN);
username.len = strlen("hr");
username.arr[username.len] = '\0';
/*
** Copy the password.
*/
strncpy((char *) password.arr, "hr", PWD_LEN);
password.len = strlen("hr");
password.arr[password.len] = '\0';
/*
** copy the dbname (using EZCONNECT syntax)
*/
strncpy((char *) dbname.arr, "oel02/XE", DB_LEN);
dbname.len = strlen("oel02/XE");
dbname.arr[dbname.len] = '\0';
/*
** register sql_error() as the error handler.
*/
EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
/*
** Connect to database. Will call sql_error()
** if an error occurs when connecting.
*/
EXEC SQL CONNECT :username IDENTIFIED BY :password USING :dbname;
printf("\nConnected to ORACLE as user: %s\n\n", username.arr);
/*
** simple select statement
*/
EXEC SQL DECLARE emps CURSOR FOR
SELECT employee_id,
first_name,
last_name
FROM employees
ORDER BY last_name,
first_name;
/*
** open the cursor
*/
EXEC SQL OPEN emps;
/*
** when done fetching break out of the for loop
*/
EXEC SQL WHENEVER NOT FOUND DO break;
/*
** simple counter variable
*/
ctr = 0;
/*
** print a little header
*/
printf("Employee ID First Name Last Name\n");
printf("=========== ==================== =========================\n");
/*
** fetch all the rows
*/
for (;;)
{
EXEC SQL FETCH emps into :empid, :fname, :lname;
/*
** null-terminate the string values
*/
fname.arr[fname.len] = '\0';
lname.arr[lname.len] = '\0';
/*
** print the current values
*/
printf("%-13d%-22s%-25s\n", empid, fname.arr, lname.arr);
ctr++;
}
/*
** close the cursor
*/
EXEC SQL CLOSE emps;
/*
** provide simple feedback on how many rows fetched
*/
printf("\nFetched %d employees.\n", ctr);
/*
** disconnect from database
*/
EXEC SQL ROLLBACK WORK RELEASE;
/*
** have a nice day
*/
exit(EXIT_SUCCESS);
}
Add a Reference to the Generated C Source File
The output of the Pro*C precompiler is either C or C++ source code (C in this case). However, because we are working with only a Pro*C source file we need to tell Visual Studio about the file that will be generated. To do this we add a reference to the not yet generated file:
- Select Project –> Add New Item to open the Add New Item dialog
- Select "Code" under "Visual C++"
- Select "C++ File (.cpp)" under "Visual Studio installed templates"
- Type "proctest.c" in the "Name" textbox and click "Add"
- Next close the (empty) file after it is created
Add the Pro*C Library File to the Project
- Select Project –> <project name> Properties… to open the Property Pages dialog
- Expand the "Configuration Properties" node
- Expand the "Linker" node
- Click the "Input" item
- In the "Additional Dependencies" type "orasql11.lib" and click "OK" to save the changes
Add the Custom Build Step
In order for Visual Studio to be able to invoke the Pro*C executable (proc.exe) to create the C source code file, a custom build step needs to be created:
- Right-click "proctest.pc" in the Solution Explorer and select "Properties" from the context menu
- Select "Custom Build Step"
- For "Command Line" type "proc.exe $(ProjectDir)$(InputName).pc"
- For "Outputs" type "$(ProjectDir)$(InputName).c"
- Click "OK" to save the custom build step
This step will cause Visual Studio to invoke proc.exe on the input file (proctest.pc) and create an output file called "proctest.c" which will then be compiled as normal. This is really the key step in the whole process I suppose. This custom build step is the "integration" of Pro*C into Visual Studio.
Build the Sample and Verify
All the hard work is now done and it is time to build the sample!
- Select Build –> Build Solution
If all has gone well you should see output similar to the following in the output window:
proctest - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
If there are errors reported you will need to investigate and correct the cause.
Upon completion of a successful build, you can execute the program and verify the results:
C:\temp\proctest\Debug>proctest
Connected to ORACLE as user: hr
Employee ID First Name Last Name
=========== ==================== =========================
174 Ellen Abel
166 Sundar Ande
130 Mozhe Atkinson
[ snip ]
120 Matthew Weiss
200 Jennifer Whalen
149 Eleni Zlotkey
Fetched 107 employees.
C:\temp\proctest\Debug>
Conclusion
Whew! That's a lot of work! As I mentioned at the beginning of this post, this is intended to be an introductory look at using Pro*C and Visual C++ 2008 Express Edition. There is, of course, much more that Pro*C can do and this simple example of selecting from the "employees" table in the "hr" schema is exactly that: a simple example. It is not intended to be a complete tutorial but perhaps it will be helpful in working with Pro*C and Visual Studio if you choose to do so. You should be able to adapt the steps here to using the "full" version of Visual Studio or Oracle Client.
If you made it this far, thanks for stopping by. I hope this was helpful in some regard.
NOTE: Some comments below were recently deleted by me at the request of the poster. I have, therefore, deleted my responses to those comments as they made no sense on their own. (1-June-2009 approximately 1:10 PM)
Posted by
Mark Williams
at
02:25
31 March 2009
jmp'ing around Win64 with ml64.exe and Assembly Language
I've been fascinated by (and interested in) assembly language for a number of years. I fondly remember my "Peter Norton Assembly" book and wish I hadn't sent it to a better home several years ago. But, hey, it was a major moving of house and I had to have as few items as possible. The sacrifices we make…
Anyway, I had some spare time this past weekend and decided to try and see if I could get a simple 64-bit assembly language program working under Win64. My goal was to write some text to a console window and exit without destroying my entire system. I managed to succeed in that endeavour and had a bit of fun along the way. My wife thinks I have a bizarre definition of fun. If you continue to read the rest of this post keep in mind I said I was "interested" in assembly. By no means is this intended to be a tutorial or guide in writing efficient and professional assembly! As the comments in the code indicate, there was no true attempt at optimisation, etc!
A couple of links that I ran across and found helpful (Links accurate as of today. Who knows what the future may bring?):
- Matt Pietrek's X64 Primer column from the May 2006 MSDN Magazine found here.
- Raymond Chen's (excellent) blog, The Old New Thing, posting entitled "The History of Calling Conventions, Part 5: AMD64" found here.
In order to make the code just a tiny bit more interesting I decided to call the "cpuid" function to get the Vendor ID from the processor and write that out to the console window. Information on the "cpuid" function is available in pdf format on both the Intel and AMD websites. One thing that I found interesting is that the 4 byte chunks (of which there are 3) that make up the output of the Vendor ID request from the "cpuid" function are stored in the registers in an "out of order" fashion. Look at the code below that gets the chunks from the registers and you'll see what I mean (I hope).
My First-Ever Windows 64-bit Assembly Language Program as a Learning Experience / Exploration
NOTE: Multiple edits made per comment(s) etc. over time and things may change (perhaps dramatically) in the future
The command I used to compile the above is (apologies if wrapping make it difficult to read):
ml64.exe test64.asm /link /subsystem:console /defaultlib:"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib\x64\kernel32.lib" /entry:main
And for the moment of truth…
C:\My Projects\Test\ASM\test>test64.exe
The processor Vendor ID is 'GenuineIntel'.
Number of bytes written: 46
Hurray! Unless you sell AMD CPUs then you might think 'AuthenticAMD' would look better…
If you see something profoundly wrong in the above, please let me know. It's not as if I am going to be churning out assembly code all of a sudden, but I would still be interested in knowing… that is, after all, the purpose of this exploration – to understand better.
Posted by
Mark Williams
at
00:51
25 March 2009
ODP.NET Tip: Bind Variables, the BindByName Property, and PL/SQL Function Return Values
A question was posed as a follow-up to a previous post here:
http://oradim.blogspot.com/2007/08/odpnet-tip-bind-variables-and.html
The poster wanted to know if you have set BindByName to "true" for an OracleCommand object and the command text for that object calls a PL/SQL function (which of course has a return value but no name), what name should you give to the parameter object that will receive the return value? It's a good question since BindByName set to "true" implies that you've given names to your bind variables!
The short answer is: It doesn't matter as it will be ignored anyway.
As in The Highlander movies, with return values, there can be only one. You can set ParameterDirection to "ReturnValue" for multiple parameters, but it is an error to do so. Try it and see what errors are raised.
Here's a quick little demo showing that it does not matter what name you give to the parameter that you declare as the return value.
First create a simple PL/SQL function (so that there is a return value!) like this:
create or replace function subtract_numbers(p1 in number, p2 in number) return number as
begin
return p1 - p2;
end;
/
I've elected to do a simple bit of subtraction and this is for a reason. Since in subtraction the order of the parameters matters, it is easy to show that the parameters are being handled in the correct order. As you'll see in the sample code below, I use the values "48" and "2" so it would be obvious if the ordering were reversed.
Now, here's some simple (and normal caveats about no error handling, etc. apply) bit of C# to illustrate the point:
using System;
using System.Data;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
class Program
{
static void Main(string[] args)
{
// adjust connect string to suit your environment
string constr = "user id=hr;" +
"password=hr;" +
"data source=orademo;" +
"pooling=false;" +
"enlist=false";
// create and open connection
OracleConnection con = new OracleConnection(constr);
con.Open();
// create command object
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "subtract_numbers";
cmd.CommandType = CommandType.StoredProcedure;
// set bindbyname to true so parameters can be added
// in any order and to illustrate what happens with a
// return value
cmd.BindByName = true;
// the first input parameter to the pl/sql function
OracleParameter p1 = new OracleParameter();
p1.Direction = ParameterDirection.Input;
p1.OracleDbType = OracleDbType.Int64;
p1.ParameterName = "p1";
p1.Value = 48;
// the second input parameter to the pl/sql function
OracleParameter p2 = new OracleParameter();
p2.Direction = ParameterDirection.Input;
p2.OracleDbType = OracleDbType.Int64;
p2.ParameterName = "p2";
p2.Value = 2;
// the return parameter from the pl/sql function
OracleParameter p3 = new OracleParameter();
p3.Direction = ParameterDirection.ReturnValue;
p3.OracleDbType = OracleDbType.Int64;
p3.ParameterName = "ignored";
// add the parameters in a "wrong" order since
// bindbyname is true -- this is key
cmd.Parameters.Add(p1); // input #1
cmd.Parameters.Add(p3); // return value
cmd.Parameters.Add(p2); // input #2
// execute the stored pl/sql code
cmd.ExecuteNonQuery();
// write the result to the console window
Console.WriteLine("The difference of {0} and {1} is {2}",
p1.Value, p2.Value, p3.Value);
// prevent console window from closing when run from VS
Console.WriteLine();
Console.Write("ENTER to continue...");
Console.ReadLine();
// clean up
p3.Dispose();
p2.Dispose();
p1.Dispose();
cmd.Dispose();
con.Dispose();
}
}
Notice how the parameters are added to the parameter collection "out of order". This is OK since BindByName is set to "true". You can comment out the line that sets BindByName and see an exception raised if you want. Anyway, when I execute this code, I get the expected results as follows:
The difference of 48 and 2 is 46
ENTER to continue...
So, even though the return value parameter was added to the collection in the second position everything works as desired. Yes, but I gave it a name of "ignored" so how do we know that name is not really being used? Well there are two easy ways to find out. One, just don't give the parameter a name at all and everything should continue to execute just fine. It's easy to ignore a name that's not there! Or, two, if you are an inquisitive type, enable SQL tracing (search the web for 10046 trace -- you'll find a million hits) and see what ODP.NET actually sent to the database. Here's a snippet of a trace file from the above code (with the parameter name of "ignored" in place):
PARSING IN CURSOR #2 len=54 dep=0 uid=82 oct=47 lid=82 tim=10601980885 hv=1771638930 ad='7ff39775518' sqlid='gx0kvypntk44k'
Begin :ret := subtract_numbers(p1=>:v0, p2=>:v2); End;
END OF STMT
PARSE #2:c=0,e=0,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=0,tim=10601980885
BINDS #2:
Bind#0
oacdty=02 mxl=22(21) mxlc=00 mal=00 scl=00 pre=00
oacflg=03 fl2=1000000 frm=00 csi=00 siz=72 off=0
kxsbbbfp=1bb2dd60 bln=22 avl=00 flg=05
Bind#1
oacdty=02 mxl=22(21) mxlc=00 mal=00 scl=00 pre=00
oacflg=03 fl2=1000000 frm=00 csi=00 siz=0 off=24
kxsbbbfp=1bb2dd78 bln=22 avl=02 flg=01
value=48
Bind#2
oacdty=02 mxl=22(21) mxlc=00 mal=00 scl=00 pre=00
oacflg=03 fl2=1000000 frm=00 csi=00 siz=0 off=48
kxsbbbfp=1bb2dd90 bln=22 avl=02 flg=01
value=2
See how ODP.NET generated a little anonymous block and substituted "ret" for the return value parameter? You should see the same thing even if there is no name given to the return parameter. You can also clearly see how the input parameter names are associated with the input values.
Posted by
Mark Williams
at
01:16
14 March 2009
HotSos Symposium 2009 Materials
The paper and all sample code from my HotSos Symposium 2009 presentation entitled "Instrumented Code Is Better Code" is available as a single .zip file. To download the .zip file, click here.
Posted by
Mark Williams
at
18:14
20 January 2009
Oracle ACE Director No More
Today marks my first day back at Oracle Corp. and I find myself in the same boat as Kevin Closson. I'm not saying I'm sitting in the same section mind you, just that I am in the same boat as regards to no longer being eligible for the Oracle ACE program.
I think I have removed the ACE and Director logos as well as text from the appropriate spots, but if I missed a place or two, well, I'll try to get them removed too!
As Kevin says, at least there is that vest thing...
Posted by
Mark Williams
at
22:16
11 January 2009
So, back to Oracle then?
Next week will mark the last week at my current location as I have recently decided to return to Oracle Corp.
I will be going back to the world of C and UNIX derivatives having accepted a position on the non-Windows PLBDE team.
It's hard to say what that will mean for the blog here, which has been pretty quiet from my side as of late to be sure, but perhaps I will branch out from my typical topics of ODP.NET and/or Oracle on Windows. We'll see.
Posted by
Mark Williams
at
01:09
08 November 2008
HotSos Symposium 2009
The HotSos Symposium 2009 is coming up soon. For 2009, the symposium runs from 8 March through to 12 March. This is always one of my favorite conferences of the year, and I will be presenting on "Instrumented Code is Better Code".
The conference, as always, features a fantastic line-up this year. I'm particularly interested in the keynote being delivered by Chris Date. In addition, Jonathan Lewis will be delivering the training day material. If you have the opportunity to attend this conference, I highly recommend it.
Now, I'd better get to work!
Posted by
Mark Williams
at
01:12
17 October 2008
Does ODP.NET Send Data to the Server for Out PL/SQL Parameters?
I was recently discussing (and by that I mean trading emails) with a friend about an interesting behaviour observed when using PL/SQL "out" VARCHAR2 variables with ODP.NET - in this case it looks like ODP.NET is sending data from the client and to the server for a parameter that is defined as "out" in a PL/SQL procedure. This seems like strange behaviour and I don't have a good answer as to why data would be sent from the client to the server for a parameter declared as an "out" parameter.
In any case here's a quick and dirty sample that illustrates this (on my test system anyway):
PL/SQL Procedure
create or replace procedure ptest (p_vc out varchar2) as
begin
p_vc := rpad ('a', 31999, 'a');
end;
/
C# Sample Console
using System;
using System.Data;
using System.Data.OracleClient;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
namespace Miscellaneous {
class Program {
static void Main(string[] args) {
string constr = "User Id=markwill;" +
"Password=oracle;" +
"Data Source=orademo;" +
"Pooling=false;" +
"Enlist=false";
TestODP(constr);
TestMS(constr);
Console.WriteLine();
Console.Write("ENTER to continue...");
Console.ReadLine();
}
static void TestODP(string constr) {
Oracle.DataAccess.Client.OracleConnection con = new Oracle.DataAccess.Client.OracleConnection(constr);
con.Open();
Oracle.DataAccess.Client.OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "alter session set events '10046 trace name context forever, level 12'";
cmd.ExecuteNonQuery();
cmd.CommandText = "ptest";
cmd.CommandType = CommandType.StoredProcedure;
cmd.BindByName = true;
Oracle.DataAccess.Client.OracleParameter p_vc = new Oracle.DataAccess.Client.OracleParameter();
p_vc.ParameterName = "p_vc";
p_vc.OracleDbType = OracleDbType.Varchar2;
p_vc.Size = 32000;
p_vc.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p_vc);
cmd.ExecuteNonQuery();
Console.WriteLine("p_vc = {0}", p_vc.Value);
p_vc.Dispose();
cmd.Dispose();
con.Dispose();
}
static void TestMS(string constr) {
System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(constr);
con.Open();
System.Data.OracleClient.OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "alter session set events '10046 trace name context forever, level 12'";
cmd.ExecuteNonQuery();
cmd.CommandText = "ptest";
cmd.CommandType = CommandType.StoredProcedure;
System.Data.OracleClient.OracleParameter p_vc = new System.Data.OracleClient.OracleParameter();
p_vc.ParameterName = "p_vc";
p_vc.OracleType = OracleType.LongVarChar;
p_vc.Size = 32000;
p_vc.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p_vc);
cmd.ExecuteNonQuery();
Console.WriteLine("p_vc = {0}", p_vc.Value);
cmd.Dispose();
con.Dispose();
}
}
}
This is pretty straight-forward and simply opens a connection, enables Extended SQL Tracing via setting event 10046, creates a parameter for the output value, and calls the stored procedure.
Looking at the respective trace files I find this for the ODP.NET test:
PARSING IN CURSOR #4 len=28 dep=0 uid=88 oct=47 lid=88 tim=9306513611 hv=1721325005 ad='7ff2eca8c38' sqlid='dk08s2dm9kpfd'
Begin ptest(p_vc=>:v0); End;
END OF STMT
PARSE #4:c=0,e=798,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=9306513607
WAIT #4: nam='SQL*Net more data from client' ela= 48 driver id=1413697536 #bytes=44 p3=0 obj#=-1 tim=9306513786
WAIT #4: nam='SQL*Net more data from client' ela= 7 driver id=1413697536 #bytes=24 p3=0 obj#=-1 tim=9306513827
WAIT #4: nam='SQL*Net more data from client' ela= 171 driver id=1413697536 #bytes=4 p3=0 obj#=-1 tim=9306514027
WAIT #4: nam='SQL*Net more data from client' ela= 28 driver id=1413697536 #bytes=49 p3=0 obj#=-1 tim=9306514085
BINDS #4:
Bind#0
oacdty=01 mxl=32767(32000) mxlc=32000 mal=00 scl=00 pre=00
oacflg=03 fl2=1000010 frm=01 csi=178 siz=32767 off=0
kxsbbbfp=1c3d8000 bln=32767 avl=32000 flg=05
value="Ä6¿¿"...
WAIT #4: nam='SQL*Net message to client' ela= 3 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=9306514373
WAIT #4: nam='SQL*Net more data to client' ela= 49 driver id=1413697536 #bytes=8216 p3=0 obj#=-1 tim=9306514456
WAIT #4: nam='SQL*Net more data to client' ela= 23 driver id=1413697536 #bytes=8192 p3=0 obj#=-1 tim=9306514507
WAIT #4: nam='SQL*Net more data to client' ela= 271 driver id=1413697536 #bytes=8192 p3=0 obj#=-1 tim=9306514805
EXEC #4:c=0,e=1139,p=0,cr=0,cu=0,mis=0,r=1,dep=0,og=1,tim=9306514839
XCTEND rlbk=0, rd_only=1
WAIT #4: nam='SQL*Net message from client' ela= 69517 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=9306584449
XCTEND rlbk=0, rd_only=1
As you can see there are several "SQL*Net more data from client" messages. Of course, these are followed by the sending of data to the client.
Now here is the relevant portion of the trace file for the MS provider test:
PARSING IN CURSOR #3 len=30 dep=0 uid=88 oct=47 lid=88 tim=9306615423 hv=1003497284 ad='7ff27721950' sqlid='42zzxzhxx09u4'
begin ptest(p_vc=>:p_vc); end;
END OF STMT
PARSE #3:c=0,e=1507,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=9306615420
BINDS #3:
Bind#0
oacdty=01 mxl=32767(32000) mxlc=32000 mal=00 scl=00 pre=00
oacflg=03 fl2=1000010 frm=01 csi=178 siz=32767 off=0
kxsbbbfp=1c3d8000 bln=32767 avl=00 flg=05
WAIT #3: nam='SQL*Net message to client' ela= 3 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=9306615726
WAIT #3: nam='SQL*Net more data to client' ela= 69 driver id=1413697536 #bytes=8216 p3=0 obj#=-1 tim=9306615822
WAIT #3: nam='SQL*Net more data to client' ela= 18 driver id=1413697536 #bytes=8192 p3=0 obj#=-1 tim=9306615861
WAIT #3: nam='SQL*Net more data to client' ela= 200 driver id=1413697536 #bytes=8192 p3=0 obj#=-1 tim=9306616081
EXEC #3:c=0,e=612,p=0,cr=0,cu=0,mis=0,r=1,dep=0,og=1,tim=9306616112
XCTEND rlbk=0, rd_only=1
WAIT #3: nam='SQL*Net message from client' ela= 60351 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=9306676628
XCTEND rlbk=0, rd_only=1
In this case there are no messages from the client and in the bind section there is no "value=" element.
An interesting wrinkle is that if I set the parameter direction to "ParameterDirection.InputOutput" using ODP.NET I do not observe the sending of data from the client.
Why, I wonder, does ODP.NET send data to the server for an output parameter? Ideas? Similar behaviour observed?
Posted by
Mark Williams
at
00:27
25 September 2008
Oracle Open World 2008 Podcast
I've never really been a prolific blogger and the "interruption" of OOW 2008 has definitely impacted this. However, my podcast with Tom Haunert of Oracle Magazine is now available at the following location:
Oracle Magazine Feature Casts
The title of the podcast is:
Origins of a .NET Developer
Posted by
Mark Williams
at
19:40
28 August 2008
My Current Development Environment
Whilst I am primarily a DBA during the day, one of the things that I really enjoy about my current engagement is that it has given me the opportunity to dip my toes back in the development pool a bit. I was describing my current development environment to a friend recently and this person was shocked by the details. I realise that in this day and age of code spitters (term coined by Jeff Richter?) and "click through the wizard for instant coding success" sorts of activities that this environment is something of an anachronism.
Without further ado, the o/s:
$ uname -a
HP-UX zzz B.11.31 U ia64 4131905082 unlimited-user license
My "IDE":
$ which vi
/usr/bin/vi
And the compiler:
$ gcc -v
Using built-in specs.
Target: ia64-hp-hpux11.31
Configured with: ../gcc/configure
Thread model: posix
gcc version 4.2.1
Yes, I actually have to do things like create a Makefile in such an environment!
Joking aside though, one of the things that I firmly believe is that instrumented code is better code. One of the characteristics of this kind of development environment, I feel, is that it really encourages strongly instrumenting the code. Yes, in some circumstances I could use the gdb debugger, but building proper instrumentation into the code and offering a simple switch to enable it (yes, it is in the release build) works very nicely.
Another thing that some people might find "weird" about this application is that it is a database application (Oracle of course) that contains absolutely no SQL statements. Really. It is a "thick database" application (Toon Koppelaars term?). That is, there are many statements like "begin package.procedure...; end;" in the code. All the real work is done using PL/SQL packages (and bodies) in the database.
As a .NET developer it is often easy to lean heavily on Visual Studio for debugging, but I think Microsoft also agrees with the "instrumented code is better code" philosophy - check out the System.Diagnostics.Trace class in the Visual Studio Combined Help Collection for more information.
Lastly, if I've misappropriated any of the terms above, apologies!
Posted by
Mark Williams
at
01:54
23 August 2008
Oracle OpenWorld 2008 - Just Say Hi!
Just a quick note... if you happen to be coming to Oracle OpenWorld 2008 in San Francisco, I will be helping out in the .NET Hands-On Labs Monday (22 Sep) afternoon. I'll also be at the "OTN ACE Office Hours" on Monday (22 Sep) as well as Thursday (25 Sep). See the link for schedules as well as the great folks who will be available. I've also been known to be around the demo grounds booth as well. Please say "hi" if you get the chance!
Posted by
Mark Williams
at
01:35