Introduction
This article is intended to show how to access the Oracle database using the ADO.NET programming model and Oracle Data Provider for .NET (ODP.NET) that Oracle makes available.
We discuss a common business scenario where we have an Oracle database as a backend server and a Windows client application querying this database system. Oracle is one of the leading database vendors and the ADO.NET model provides an interface to develop applications regardless of the underlying data source. ODP.NET is the data provider supported by Oracle and implements several Oracle database's specific features. Although, Microsoft.NET framework ships with an Oracle database's ADO.NET provider and both of the providers will satisfy the needs of most applications, and in this article, I will focus on the ODP.NET provider.
ODP.NET
- In Visual Studio.NET go to the main menu and select File|New|Project and from the New Project window choose Windows Application template, enter ODPWinClient for the name, and enter a directory to store the project.
- Add a reference to the assembly Oracle.DataAccessClient.dll selecting Project|Add Reference... from the main menu.
- Add a DataGridView control from the toolbox to the form. Name it m_dgvViewer and one Button control.
- Add the following namespace declaration.
- using Oracle.DataAccess.Client;
- using Oracle.DataAccess.Types;
-
The most important object is the connection instance of class OracleConnection. Prior to connecting to an Oracle Database using ODP.NET, you should add Net Service Names. You need to update the file tnsnames.ora in your local ORACLE_HOME\network\admin directory by adding entries that would be the data source list. You can edit this file manually, or using the Oracle Net Configuration tool which walks you through some screens gathering required connection information. The code for creating the connection is shown below.
- string strConn = "Data Source=ORCL; User Id=scott; Password=tiger";
- OracleConnection objConnection = new OracleConnection();
- objConnection.ConnectionString = strConn;
In this case, we have configured the file tnsnames.ora as shown below to connect the sample database ORCL using the well-known user Scott and password tiger.- ORCL =
- (DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = yourhost)(PORT = 1521))
- ) {
- CONNECT_DATA =
- (SERVICE_NAME = ORCL)
- }
- )
There are some connection string properties specific to ODP.NET providers such as DBA Privilege which sets to SYSDBA or SYSOPER to request administrative privileges, ValidateConnection, StatementCachePurge which causes the statement cache to be purged when the connection is closed, StatementCacheSize, ProxyUserId, ProxyPassword, IncrPoolSize, and DecrPoolSize. -
You can retrieve information about departments in the ORCL database system (this is an illustrative database system for managing human resources' data) using the following code.
- string strConn = "Data Source=ORCL; User Id=scott; Password=tiger";
- using(OracleConnection objConnection = new OracleConnection()) {
- objConnection.ConnectionString = strConn;
- try {
- objConnection.Open();
- OracleCommand objCommand = new OracleCommand();
- objCommand.Connection = objConnection;
- objCommand.CommandText = "select deptno, dname, loc from dept";
- objCommand.CommandType = System.Data.CommandType.Text;
- OracleDataAdapter objAdapter = new OracleDataAdapter(objCommand);
- DataTable objTable = new DataTable();
- objAdapter.Fill(objTable);
- this.m_dgvViewer.DataSource = objTable;
- objConnection.Close();
- } catch (Exception ex) {
- System.Windows.Forms.MessageBox.Show(ex.ToString());
- } finally {
- objConnection.Close();
- }
- }
-
You use a bind variable to include the value of the text box as part of the SELECT statement. For example, we retrieve information about a particular department as shown below. In this case the SQL SELECT Statement has some parameters. As you can see the parameter format in Oracle provider, it is used the ":" character, is different to SQL Server provider, while it is used the "@" character. Update operation is done similar using the ADO.NET model.
- string strConn = "Data Source=ORCL; User Id=scott; Password=tiger";
- using(OracleConnection objConnection = new OracleConnection()) {
- objConnection.ConnectionString = strConn;
- try {
- objConnection.Open();
- OracleCommand objCommand = new OracleCommand();
- objCommand.Connection = objConnection;
- objCommand.CommandText = "select deptno, dname, loc from dept where deptno =: deptnoparam ";
- objCommand.CommandType = System.Data.CommandType.Text;
- OracleParameter objDeptNoParam = new OracleParameter("deptnoparam", OracleDbType.Int16);
- objDeptNoParam.Value = this.m_tbDeptNo.Text.Trim();
- objCommand.Parameters.Add(objDeptNoParam);
- OracleDataAdapter objAdapter = new OracleDataAdapter(objCommand);
- DataTable objTable = new DataTable();
- objAdapter.Fill(objTable);
- this.m_dgvViewer.DataSource = objTable;
- objConnection.Close();
- } catch (Exception ex) {
- System.Windows.Forms.MessageBox.Show(ex.ToString());
- } finally {
- objConnection.Close();
- }
- }

Hazel ZamperiniPosted Jul 19, 2012, 9:39 AM
Hi Have you ever deployed connection pooling with Oracle users that are kerberos authenticated via AD? We seem to have intermittent ora-12638 errors that make the site unusable until we disable connection pooling. The sqlnet traces show where the first couple of usernames being passed are the actual legit user, then for some reason the connection switchs over to the connection pool name.... and of course, the pool name is not a legit database user and hence throws the 12638 error.
Alberto NetoPosted Jun 5, 2009, 3:51 PM
Hi John! I´ve this issue. How can i deploy an application c# using odp.net on a client machine without installing odp ?? I tried just copy oracle.dataaccess.dll to the same directory of .exe file but it wasn´t work. Gimme some light on it, please. I´m using database xe 10g and .net framework 2.0. Thanks for listenning. Alberto. Oracle Analyst & Developer
SnehaPosted Sep 5, 2007, 8:08 AM
I am unable to find i am unable to find Oracle.DataAccessClient.dll while selecting add->reference I am using .net 2.0
SnehaPosted Sep 5, 2007, 7:55 AM
I am unable to find i am unable to find Oracle.DataAccessClient.dll while selecting add->reference I am using .net 2.0
TerenceeditedPosted May 6, 2007, 10:00 PMEdited May 6, 2007, 10:02 PM
Hello John, Can the tables in the returned DataSet be named specifically rather than the generic name (table, table1) provided by MS? I have some storedprocs that return multiple ref cursors and each ref cursor is a separate datatable in the dataset. I want to refer to each datatable by a specific name. I appreciate any assistance you may be able to offer. Terence