How to: Connect to an Oracle Database Using ODP.NET
Date: 19-Jun-2003
Objective
After reading this how-to document you should be able
to:
Prerequisite
The reader is assumed to be familiar with MS Visual
Studio.NET, and also possess an understanding of the rudimentaries of ODP.NET
and databases.
Introduction
ODP.NET, which is included in the Oracle.DataAccesss.dll
assembly, provides a rich collection of classes that assist in easy database
interaction. It uses Oracle's native APIs to offer fast and reliable access
to Oracle data and features from any .NET application.
In this how-to we'll look at how to use the OracleConnection
class, provided by ODP.NET, to establish a connection to an Oracle database
and interact with the database. Here, we'll use a small code fragment
to demonstrate how to connect to an Oracle database using ODP.NET.
Further, connection pooling is enabled in ODP.NET (by
default). So we'll also look at how you can control the connection pooling
parameters provided by ODP.NET.
Requirements
Description
When you install ODP.NET, the Oracle Universal Installer
automatically registers ODP.NET with the Global Assembly Cache (GAC).
The GAC in the .NET framework provides a central place for registering
assemblies. These assemblies once registered are then available to all
applications for usage, including the development environments like Visual
Studio.NET.
The most important class with respect to this how-to
is the OracleConnection class. An OracleConnection
object represents a connection to an Oracle database. In this how-to we
will demonstrate how to connect to an Oracle database and list names of
all the employees and their employee number present in a table. Alternatively,
you may use any database user account that has connect
and resource privileges to connect to the
Oracle database.
Prior to connecting to an Oracle Database using ODP.NET,
one should add Net Service Names. Specifying the Net Service Names also
known as TNS alias is used to identify an Oracle Database Instance.
Add Net Service Names
ODP.NET uses Net Service Names to
identify the data source (database) it connects to. The Oracle Net Configuration
Assistant Tool, a post installation tool that configures basic network
components, can be used to setup the Net Service Names. Please refer to
the Oracle9i
Net Services Administrator's Guide available at OTN for the details
on using this tool to setup the Net Services Names.
|