This article mainly introduced the C # connection Oracle method, combined with the example form summarizes several common C # connection Oracle database's operation skill and the related attention matters, needs the friend can refer to the next
This article summarizes how C # connects to Oracle. Share to everyone for your reference, as follows:
First, through System.Data.OracleClient (need to install Oracle client and configure Tnsnames.ora)
1. Add a namespace System.Data.OracleClient reference
Using System.Data.OracleClient;
String connstring = "User Id=ifsapp; Password=ifsapp;data source=race; "; O Racleconnection conn = new OracleConnection (connstring); try{ Conn. Open (); MessageBox.Show (Conn. State.tostring ());} catch (Exception ex) { showerrormessage (ex. Message.tostring ());} finally{ Conn. Close ();}
Second, through the System.Data.OracleClient (need to install Oracle client does not need to configure Tnsnames.ora)
1. Add a namespace System.Data.OracleClient reference
Using System.Data.OracleClient;
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 ();}
Third, through the System.Data.OleDb and Oracle Company's Drive
1. Add a namespace System.Data.OracleClient reference
Using System.Data.OleDb;
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. through System.Data.OleDb and Microsoft's Oracle Drive
1. Add a namespace System.Data.OracleClient reference
Using System.Data.OleDb;
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. Use of the ODP connection
1. Download and install Odp.net (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
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. use of third-party drivers
Third party driver has devart, download drive 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
Using Devart.Data.Oracle;
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 ();}