This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17581|Reply: 0

[Source] There are several ways C# can connect to Oracle

[Copy link]
Posted on 9/6/2016 1:20:21 PM | | |

1. Go through System.Data.OracleClient (you need to install Oracle client and configure tnsnames.ora)
1. Add a namespace reference to System.Data.OracleClient
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();
}

2. Through System.Data.OracleClient (you need to install the Oracle client and do not need to configure tnsnames.ora)
1. Add a namespace reference to System.Data.OracleClient
2. using System.Data.OracleClient;
3.
string connString = "User ID=IFSAPP; Password=IFSAPP; Data Source=(DEscrip{filtering}tION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_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: Through System.Data.OleDb and Oracle drivers
1. Add a namespace reference to System.Data.OracleClient
2. using System.Data.OleDb;
3.
string connString = "Provider=OraOLEDB.Oracle.1; User ID=IFSAPP; Password=IFSAPP; Data Source=(DEscrip{filtering}tION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_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();
}

Four: Through System.Data.OleDb and Microsoft's Oracle driver
1. Add a namespace reference to System.Data.OracleClient
2. using System.Data.OleDb;
3.
string connString = "Provider=MSDAORA.1; User ID=IFSAPP; Password=IFSAPP; Data Source=(DEscrip{filtering}tION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_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();
}

Remark:
a. The XP operating system has installed Microsoft's Oracle driver C:\Program Files\Common Files\System\Ole DB\msdaora.dll
b. The driver requires three files (oraocixe10.dll, oci.dll, and ociw32.dll) of the Oracle client to be placed under System32

Five: Use ODP connection
1. Download and install ODP.NET (http://www.oracle.com/technetwork/developer-tools/visual-studio/downloads/index.html)
2. A sequence file will be generated after the installation is completed.
3. Find this installation directory, open the folder %ORACLE_HOME%\Network\Admin and create a tnsnames.ora file under it, the contents of which can refer to the configuration under the Sample directory under it
Oracle.RACE =
(DEscrip{filter}tION=.)
   (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, which can be taken at will. The string after the equals sign can be copied from the TNS descriptor after connecting to the database in the Enterprise Manager Console tool
4. Reference 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();
}

Six: Use third-party drivers
Third-party drivers include Devart, download drivers http://www.devart.com/dotconnect/oracle/, but they are commercial versions and require purchasing a license or cracking
Connection format User ID=myUsername; Password=myPassword; Host=ora; Pooling=true; Min Pool Size=0; Max Pool Size=100; Connection Lifetime=0;
1. Reference 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();
}




Previous:Windows cannot start SQL Server (MSSQLSERVER) on a local computer. For more information, see the department...
Next:c# QQ's skey-to-bkn algorithm
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com