C # Several ways to connect to Oracle

Source: Internet
Author: User
Tags oracleconnection

One: Via System.Data.OracleClient (need to install Oracle client and configure Tnsnames.ora)
1. Add a namespace System.Data.OracleClient reference
2. Using System.Data.OracleClient;
3.
String connstring = "User Id=ifsapp; Password=ifsapp;data source=race; ";
OracleConnection conn = new OracleConnection (connstring);
Try
{
Conn. Open ();
MessageBox.Show (Conn. State.tostring ());
}
catch (Exception ex)
{
Showerrormessage (ex. Message.tostring ());
}
Finally
{
Conn. Close ();
}

II: Via System.Data.OracleClient (requires no configuration of the Oracle client Tnsnames.ora)
1. Add a namespace System.Data.OracleClient reference
2. Using System.Data.OracleClient;
3.
String connstring = "User Id=ifsapp; Password=ifsapp;data source= (DESCRIPTION = (address_list= (ADDRESS = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT = 1521))) (CO Nnect_data = (service_name = RACE))) ";
OracleConnection conn = new OracleConnection (connstring);
Try
{
    conn. Open ();
    MessageBox.Show (Conn. State.tostring ());
}
catch (Exception ex)
{
    showerrormessage (ex. Message.tostring ());
}
Finally
{
    conn. Close ();
}

three: Drive through System.Data.OleDb and Oracle company
1. Add a namespace System.Data.OracleClient reference
2. Using System.Data.OleDb;
3.
String connstring = "PROVIDER=ORAOLEDB.ORACLE.1; User Id=ifsapp; Password=ifsapp;data source= (DESCRIPTION = (address_list= (ADDRESS = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT = 1521))) (CO Nnect_data = (service_name = RACE))) ";
OleDbConnection conn = new OleDbConnection (connstring);
Try
{
    conn. Open ();
    MessageBox.Show (Conn. State.tostring ());
}
catch (Exception ex)
{
    showerrormessage (ex. Message.tostring ());
}
Finally
{
    conn. Close ();
}

IV: Oracle drive through SYSTEM.DATA.OLEDB and Microsoft Corporation
1. Add a namespace System.Data.OracleClient reference
2. Using System.Data.OleDb;
3.
String connstring = "provider=msdaora.1; User Id=ifsapp; Password=ifsapp;data source= (DESCRIPTION = (address_list= (ADDRESS = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT = 1521))) (CO Nnect_data = (service_name = RACE))) ";
OleDbConnection cnn = new OleDbConnection (connstring);
Try
{
Conn. Open ();
MessageBox.Show (Conn. State.tostring ());
}
catch (Exception ex)
{
Showerrormessage (ex. Message.tostring ());
}
Finally
{
Conn. Close ();
}

Note:
A.XP operating system has installed Microsoft's Oracle driver C:\Program Files\Common Files\System\Ole Db\msdaora.dll
B. This driver requires three files from the Oracle client (Oraocixe10.dll, Oci.dll, Ociw32.dll) to be placed under System32

V: Connect using the ODP
1. Download and install Odp.net (http://www.oracle.com/technetwork/developer-tools/visual-studio/downloads/index.html)
2. When the installation is complete, a sequence of files is generated.
3. Locate the installation directory, open the folder%oracle_home%\network\admin below this to create a Tnsnames.ora file, its contents can be referenced under the sample directory below the configuration
Oracle.race =
(description=
(address_list=
(address=
(PROTOCOL=TCP)
(host=127.0.0.1)
(port=1521)
)
)
(Connect_data=
(Sid=race)
(server=dedicated)
)
)
Oracle.race is the name of the connection string and can be arbitrarily taken. The string after the equals sign can be copied in the TNS descriptor after the database is connected in the Enterprise Manager Console tool.
4. Referencing the Oracle.dataaccess namespace
5. Using Oracle.DataAccess.Client;
6. Sample code:
String connstring = "DATA source=oracle.race; PERSIST SECURITY info=true; USER Id=ifsapp;password=ifsapp ";
OracleConnection conn = new OracleConnection (connstring);
Try
{
Conn. Open ();
OracleCommand cmd = new OracleCommand (cmdtext,conn);
OracleDataReader reader = cmd. ExecuteReader ();
This. Datagridview1.datasource = reader;
This. Datagridview1.databind ();
}
catch (Exception ex)
{
Showerrormessage (ex. Message.tostring ());
}
Finally
{
Conn. Close ();
}

VI: Using third-party drivers
Third party driver has devart, download drive http://www.devart.com/dotconnect/oracle/, but is commercial version, need to purchase license or hack
Connection format User Id=myusername; Password=mypassword; Host=ora; Pooling=true; Min Pool size=0; Max Pool size=100; Connection lifetime=0;
1. Referencing the Devart.Data.Oracle namespace
2. Using Devart.Data.Oracle;
3.
OracleConnection conn = new OracleConnection ();
Conn. ConnectionString = "";
Conn. Unicode = true;
Conn. UserId = "Ifsapp";
Conn. Password = "Ifsapp";
Conn. Port = 1521;
Conn. Server = "127.0.0.1";
Conn. Sid = "RACE";
Try
{
Conn. Open ();
Execute queries, etc
}
catch (Exception ex)
{
Showerrormessage (ex. Message.tostring ());
}
Finally
{
Conn. Close ();
}

C # Several ways to connect to Oracle

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.