Connecting To Oracle Database Using C# Without Installing Oracle Client

In this post, I will try to explain how to create a C# application which will run on a Windows client machine without “Oracle Client” installed on it and will connect to an Oracle database running on a Server.

Such an application will provide the portability where it can just be copied to and executed on any client machine on the same network as the database Server. (You may only have to upgrade the .Net Framework version on the client machine to make it comply with your application.)

The solution below will apply both to 11g and 12c databases.

Step 1
We will need a set of dlls provided by Oracle to use in our project. So at this first step, download Oracle Data Access Components (ODAC) for your version of Windows. As of the posting date of this article, the valid download URL is: http://www.oracle.com/technetwork/topics/dotnet/downloads/odacdeploy-4242173.html

Note: ODAC 32 bit version works both for 32bit and 64bit. And I had several problems when using 64bit version. So I can suggests directly downloading the 32bit version.

Open the downloaded zip file and get the dlls below together. I listed the dlls with the folder where you will find them. We will later copy them into our debug folder.

\instantclient_12_2\
oci.dll
orannzsbb12.dll
oraocci12.dll
oraocci12d.dll
oraociei12.dll
oraons.dll

\oramts\bin\
oramts.dll
oramts12.dll
oramtsus.dll

\odp.net4\odp.net\bin\4\
Oracle.DataAccess.dll

\odp.net4\bin\
OraOps12.dll

Step 2
Use Visual Studio to create a console application and copy the dlls listed in Step 1 into the same folder as your project executable, the Debug folder. Add a reference to “Oracle.DataAccess.dll” by browsing to your Debug folder where you have just copied this dll into.

Step 3
Copy the code below to your project. You will need to modify the connection string and the namespace.

using System;
using Oracle.DataAccess.Client;
namespace MyConsoleApplication
{
	class Program
	{
		static void Main(string[] args)
		{
			string connectionString = "User Id=hr; Password=hr; Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = xe)));";
			
			using (OracleConnection connection = new OracleConnection())
			{
				connection.ConnectionString = connectionString;
				try
				{
					connection.Open();
					Console.WriteLine("Oracle connection is successful!");
				}
				catch (Exception ex)
				{
					Console.WriteLine("Error : " + ex.Message);
				}
				Console.ReadLine();
			}
		}
	}
}

Step 4
Go to Build tab in project properties and set the “Platform Target” according to the platform you’ll are working on.

That’s it. You can build and run your application. If everything works fine, you will be able to access your Oracle database without an Oracle Client installation.

Add a Comment

Your email address will not be published. Required fields are marked *