C # Connecting the Oracle connection string

Source: Internet
Author: User
Tags oracleconnection

<summary>
The database connection string for Oracle.
</summary>
Private Const String connstring =


The 192.168.1.210 is the IP address of the Oracle database server
1521 is the default port number
SERVICE_NAME=ORCL means that the Oracle database service is ORCL
The user name password is followed.

To connect to an Oracle database using the System.Data.OracleClient library in. Net

1. Download and install the instant Client

Instant client can be downloaded to the Oracle website at

There are three versions of the Windows,instant client:

1) Instant Client for Microsoft Windows (32-bit)

2) Instant Client for Microsoft Windows (64-bit) Itanium

3) Instant Client for Microsoft Windows (x64)

You can see your computer's processor information by clicking Properties in the right-click menu on My Computer

Since my computer is a Win7 64-bit system, I downloaded a third package

Once the download is complete, unzip and add the address of the bin directory inside to the environment variable path.

Then add the following attribute to the environment variable:

(I put the extracted path to the D drive, the extracted folder is named Instantclient_12_1)

Nls_lang=simplified Chinese_china. Zhs16gbk

Tns_admin=d:\instantclient_12_1

Ld_library_path=d:\instantclient_12_1

2. Redhat system with Oracle installed with XSHELL4 remote connection

You need to enter the following:

1) Name: Make a name for yourself

2) Protocol: Select SSH

3) Host: Destination Host IP

4) port number: port (22)

5) login username/password

Attached: the command to log in to the database in Linux is

Sqlplus User name/password @ database name

3. Get the connection string

Find an Oracle installation address

Linux command: Echo $ORACLE _home

Tnsnames.ora in the Directory

$ORACLE _home\network\admin\

After you open Tnsnames.ora with Vim, you see that the Oracle database is configured as follows:

 

XTCSJK =    (DESCRIPTION = (        ADDRESS = (PROTOCOL = TCP) (HOST = 171.0.0.132) (PORT = 1521))        (Connect_data =            (SE RVer = dedicated)            (service_name = TestDB)))     

Based on this file, you can write the connection string that connects to the Oracle database.

Data source= (description= (address_list= (address= (protocol=tcp) (                host=171.0.0.132)                (port=1521)            )        )        (Connect_data=            (SERVICE_NAME=TESTDB)        )    ); Persist Security info=true; User id= username; password= Password

  

4. Program code

Note: Running the following code requires

1) Manually add a reference to the library System.Data.OracleClient

2) in the assembly → right-click → properties → build, change the target platform from AnyCPU to x64

Otherwise, the exception will be reported badimageformatexception (compatibility issue)

3) If you do not complete the installation of the instant client or the configuration of the environment variable, the runtime will report the exception information:

System.Data.OracleClient requires Oracle client software version 8.1.7 or later

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Method1 function call Library, need to manually add reference using System.Data.OracleClient;            Namespace Oracletest{class Program {//Read time information from the Oracle database static void Readfromoracletest () { String conn = string.                Concat (@ "Data source=", @ "(description=", @ "(address_list=",                @ "(address=", @ "(protocol=tcp)", @ "        (host=171.0.0.132) "@" (port=1521) "@") "@"        ) "@" (connect_data= ", @" (SERVICE_NAME=TESTDB) ", @"                ", @"); ", @" Persist Security info=true; ", @" user id= username; ",             @ "password= password"); Oracleconnection is marked as obsolete oracleconnection OC = new OracleConnection (conn); try {oc.                Open (); OracleCommand is marked as obsolete oraclecommand cmd = oc.                CreateCommand ();                Cmd.commandtext = "Select sysdate from Dual"; OracleDataReader ODR = cmd.                ExecuteReader (); while (ODR. Read ()) {Console.WriteLine (ODR. Getoracledatetime (0).                ToString ()); }} catch (Exception ex) {Console.WriteLine (ex).            Message); } finally {oc.            Close ();            }} static void Main (string[] args) {readfromoracletest ();        Console.ReadLine (); }    }}

  

5. Running the sample

C # Connecting the Oracle connection string

Related Article

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.