Friday, April 17, 2009

Deploying ODP.NET with Oracle Instant Client

While ago I wrote about choosing right Data Provider to access Oracle. That time's choice was OleDB data provider from Microsoft.

A bit of history
Its main advantage was zero installation (the provider comes with .NET runtime) plus moderate functionality. On the downside it doesn't support SYSDBA connections, unable to configure fetch buffer size, and has no adequate method to retrieve Oracle error number from Exception. The provider still needs Oracle Client software to be installed and configured on the user's machine.

Oracle's .NET Provider came in 174M package and contained ODP.NET dlls along with full-blown client. At that time Oracle also shipped Instant Client which was "only" 80M in size, but required no installation. And the major pitfall was that ODP.NET didn't work with the Instant Client.

Finally in 2008 Oracle released production version of ODP.NET provider for NET 2.0 which could work over Instant Client. To developers, this means we can now enjoy all the luxuries of genuine ODP.NET and relax prerequisite requirements. We can deploy all components needed to access databases with XCOPY method without messing with client machine's registry or other settings.

What's discussed
In this post we will create a small Visual Studio project for program fetching sysdate from Oracle database. Connection settings will be hardcoded for simplicity. In the coming post we will write a complete program connecting to any Oracle database and running arbitrary SQL statement passed as a parameter - with no need for any Oracle software installed on the client.

Only 32-bit clients are discussed, 64-bit components for 11.x are not yet available at the moment of writing. Also we only bother about supporting English language.

ETL stands for Extract, Trash, and depLoy
Deployment files come from ODP.NET and Instant Client, so create a directory for extracted DLLs. They will be shipped to client as part of our XCOPY deployment.

First, we'll need to download ODP.NET provider. It is coming bundled along with other Oracle Data Access Components (ODAC) and available on Oracle OTN site. Registration is required to download software, but it is free and if you work with Oracle, registering at OTN is a good idea anyway. Get "With XCOPY deployment" version. At the moment the available version is, size 43M.

It comes in a ZIP archive, unpack it to temp directory. There is readme file there instructing to run install.bat but there is no need to do that. In subdirectories of ODP.NET20 locate and copy OraOps11w.dll and Oracle.DataAccess.dll to your deployment directory. You can erase downloaded file and unpacked directory now. Yes, we only need 2 files from the entire installation. They are ODP.NET provider, size ~1.3M.

Second, get Instant Client. Our provider is, but client can be of any version newer than 11.6. Right now version is available. There are 2 versions listed at the top: Instant Client Package - Basic and Basic Lite. It is up to you what version to pick; ODP.NET works happily with both. Lite version is significantly smaller, but supports limited national languages and client charsets:

  • It supports US7ASCII, WE8DEC, WE8ISO8859P1, WE8MSWIN1252, UTF8, AL16UTF16, and AL32UTF8 character sets.
  • It can connect to databases with charsets US7ASCII, WE8DEC, WE8ISO8859P1, WE8MSWIN1252, WE8EBCDIC37C, WE8EBCDIC1047, UTF8, and AL32UTF8. All client-side messages are in English.

Basic is going to add ~105M to your distribution; Basic Lite is ~32M.

Upon downloading the client, copy 3 DLLs from it to our deployment directory. For Basic version they are: oci.dll, orannzsbb11.dll, and oraociei11.dll (111M). For Lite version they are oci.dll, orannzsbb11.dll, and oraociicus11.dll (~31M). Even though first 2 names are the same, file sizes differ - so don't mix up DLLs from different distributions.

In your deployment directory there should be 5 files now: 2 Data Provider DLLs and 3 Instant Client ones. This is all what's needed to access Oracle.

Writing test app
Create Visual Studio Console project and in Solution Explorer "Add Reference" from References subtree or Project context menu. Navigate to your deployment directory and select Oracle.DataAccess.dll. Make sure its "Copy Local" property is true (so file will be copied to output directory).

Now add remaining DLLs: right-click on project and "Add existing item". They also should be deployed to client, so set property "Copy to Output Directory" to "Copy if newer" for each item.

We are now ready to write simple test code:

using System;
using Oracle.DataAccess.Client;

namespace InstantClientApp {
class Program {
static void Main(string[] args) {
OracleConnection conn = new OracleConnection("User Id=scott;Password=tiger;Data Source=machine_name/service_name");
OracleCommand cmd = new OracleCommand("select sysdate from dual", conn);
DateTime dtm = (DateTime)cmd.ExecuteScalar();
Console.WriteLine("Database time is {0}", dtm.ToString("F"));

Replace connection string parameters with valid values.

Build the project. Your Debug or Release directory should now contain 5 DLLs, the executable, and a couple of other now unneeded files generated by Visual Studio. This is it, a complete application packaged along with all what's needed to access Oracle, and ready to be deployed to the client. Try XCOPY deployment now: copy the files to another machine with no Oracle client installed, only .NET 2.0 runtime is needed. Run it, and if connection string is right, you should get database server time. Warning: it won't work from network share, you should really copy files to client's local drive.

Volume in drive C has no label.
Volume Serial Number is 30A5-4F0E

Directory of C:\Tmp

04/17/2009 10:00 AM 16,384 InstantClientApp.exe
10/01/2008 04:22 AM 520,192 oci.dll
12/20/2007 03:53 AM 917,504 Oracle.DataAccess.dll
09/18/2008 10:47 PM 1,130,496 orannzsbb11.dll
10/01/2008 04:59 AM 29,802,496 oraociicus11.dll
12/20/2007 04:02 AM 385,024 OraOps11w.dll
6 File(s) 32,772,096 bytes

Database time is Friday, April 17, 2009 9:09:09 AM

Connection string variations
You've probably noted we used EZCONNECT method to specify database: machine_name/service_name. This form was a short version of:

Instant client also recognizes full descriptor format:

If Oracle Client software is already installed on the machine, Instant Client can employ TNSNAMES.ORA too. There are two methods to do that: either set environment variable TNS_ADMIN to path to network\admin directory, or set variable ORACLE_HOME to point to installation home and tnsnames.ora will be looked up in %ORACLE_HOME%\network\admin.

More exotic methods such as LDAP or LOCAL variable are also supported, but I haven't tried them.

Environment variables
There are several environment variables which may affect your application. To be completely isolated from machine's configuration, override them manually before opening connection:

Environment.SetEnvironmentVariable("ORA_TZFILE", null);
Environment.SetEnvironmentVariable("NLS_LANG", "AMERICAN_AMERICA.AL32UTF8");
Environment.SetEnvironmentVariable("NLS_DATE_FORMAT", "DD-MON-RR");
Environment.SetEnvironmentVariable("NLS_TIME_FORMAT", "HH.MI.SSXFF AM");
Environment.SetEnvironmentVariable("NLS_TIMESTAMP_FORMAT", "DD-MON-RR HH.MI.SSXFF AM");
Environment.SetEnvironmentVariable("NLS_TIMESTAMP_TZ_FORMAT", "DD-MON-RR HH.MI.SSXFF AM TZR");

Not sue if this is a complete set though.

Will AlderPump switch to Instant Client? Right now it doesn't seem very likely. There are several reasons:

  • The product's audience is DBAs or Oracle professionals who definitely have Oracle Client installed on their machines. Demand to have at least client software configured is not an obstacle for them.
  • Switching from MS provider would involve code rewrite. Not major, but visible. And the only real feature missing is ability to connect AS SYSDBA which is not a good idea anyways.
  • AlderPump 2.1 installation is 410K. Instant Client Lite is 32M. The numbers don't look quite right together.
  • The technology may not be mature enough yet, there were too few versions released so far. It probably needs more polishing.

Having said that, I'll definitely will keep eye on it and who knows? One day the gains may outweigh the cons. I kind of hope they will, because feature-wise Oracle's ODP.NET is really good.


  1. OTN forum discussing deployment.
  2. Section OCI Instant Client of Oracle® Call Interface Programmer's Guide.
  3. OTN article Instant ODP.NET Deployment by Mark A. Williams.

No comments: