Connect to an Oracle database without an Oracle Client

Reducing the number of tasks during deployment of a new system or version update does, at least, 2 things.

  1. Fewer number of tasks means it takes less time
  2. Fewer number of tasks means less items to troubleshoot if something doesn’t work

Removing the installation and configuration of an Oracle Client can now be avoided from your installation process.

Oracle has created class library called: ODP.NET

ODP.NET can and should be used in places of “using System.Data.OracleClient” because Microsoft has said they are deprecating it. This makes sense to me because why not let the DBMS owner write the library for you.




Here is how I configured my C# environment to run ODP.NET without installing an Oracle client.

  • I downloaded Oracle Data Access Components (ODAC) 11.2.0.1.2 – (xcopy version – ODAC112012Xcopy.zip)
  • Copied OraOps11w.dll found in the odp.net4/bin directory to the directory of my C# system
  • Copied oci.dll, orannzsbb11.dll and oraociicus11.dll found in the instantclient_11_2 directory to the directory of my C# system
  • NOTE that oraociicus11.dll is English only (~32MB), while the oraociei11.dll support multiple languages and is (~123MB)
  • Copied the Oracle.DataAccess.dll found in the odp.net4/odp.net/bin/4 directory to the directory of my C# system
  • I downloaded and installed Microsoft .Net Framework v4.03019

Below is a example of how to write a simple program to test this out, be sure to add Oracle.DataAccess.dll as a reference in your C# project.

[sourcecode language="csharp" padlinenumbers="true" autolinks="false" gutter="false" toolbar="false"]
using System;
using Oracle.DataAccess.Client;
 
namespace NoOraClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //You need to enter a valid Oracle connection 
            //string, below is the format
            string connectionString = 
                "user id=USERID;password=PASSWORD;" +
                "data source=(DESCRIPTION=(ADDRESS="+
                "(PROTOCOL=tcp)(HOST=IPorSERVERNAME)" +
                "(PORT=1521))(CONNECT_DATA=" +
                "(SERVICE_NAME=ValidSID)))";
 
            using (OracleConnection connection = 
                new OracleConnection())
            {
                connection.ConnectionString = 
                    connectionString;
 
                try
                {
                    connection.Open();
                    Console.WriteLine
                        ("Connection Successful!");
                    //stops the console from closing 
                    //until you hit the ENTER key
                    Console.ReadLine();  
                }
                catch (OracleException ex)
                {
                    Console.WriteLine(ex.ToString());
                     // stops the console from closing 
                     //until you hit the ENTER key
                    Console.ReadLine(); 
                }
            }            
        }
    }
}
[/sourcecode]

image




On a side note, I always like to use USING clauses because when you create instances of objects within them you do not need to worry about closing them or de-allocating them. The garbage collector will do this on all objects instantiated within a USING clause once the focus has left it. Doing it like this can avoid memory leaks.

Download the source here.

Leave a Comment

Your email address will not be published.