C # how to connect to the Oracle database using ODP.net

Source: Internet
Author: User
Tags oracleconnection
This article describes how to connect C # To an Oracle database using ODP.net. For more information, see

This article describes how to connect C # To an Oracle database using ODP.net. For more information, see

This article describes how C # connects to the Oracle database. Through the Oracle. DataAccess. dll in instant client and ODP.net, We can conveniently deploy. net applications or sites without installing the Oracle client. Next we will introduce this process.

1. Install ODAC
Download the corresponding oracle version ODAC from the official oracle website.
: ODAC Download
Download the package and decompress the package. You do not need to install all the components during installation. Install the following components:
Oracle Instant Client
Oracle Data Provider For. net2.0
Oracle rovider For Asp. net

2. environment variable settings
Set Windows environment variables:
ORACLE_HOME: ODAC installation directory (similar ~ \ App \ Administrator \ product \ 11.1.0 \ client_1 );
LD_LIBRARY_PATH: % ORACLE_HOME %;
TNS_ADMIN: % ORACLE_HOME %;
Append at the beginning of PATH: % ORACLE_HOME %;

3. Configure tnsnames. ora in the listener File
Create the tnsnames. ora file in the directory % ORACLE_HOME %. The content is as follows:
The Code is as follows:
Database SID =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP) (HOST = Oracle HOST name or IP) (PORT = 1521 ))
)
(CONNECT_DATA =
(SERVICE_NAME = database SID)
)
)

4. plsqldev
After this configuration, plsqldev can connect to the oracle database.

5. C # connect to Oracle
C # sample code for connecting to oracle:
The Code is as follows:
OracleConnection conn =
New OracleConnection ();
Try
{
Conn. ConnectionString = ConfigurationManager. ConnectionStrings ["oradb"]. ConnectionString;
Conn. Open ();
String SQL = "select id, content from test"; // C #
OracleCommand cmd = new OracleCommand (SQL, conn );
Cmd. CommandType = CommandType. Text;
OracleDataReader dr = cmd. ExecuteReader (); // C #
List Contents = newList ();
While (dr. Read ())
{
Contents. Add (dr ["content"]. ToString ());
}
ListBox1.ItemsSource = contents;
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Conn. Clone ();
}

Append the database connection configuration in app. config or web. config.
The Code is as follows:

(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.1.1) (PORT = 1521 )))
(CONNECT_DATA = (SERVICE_NAME = ****)));
User Id = ***; Password = ***; "/>


Follow these steps to connect to the database successfully without errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.