1. Add Oracle.ManagedDataAccess.dll
2. The instance connecting Oracle is added to the Oracle listener, or the error "Ora-12514:tns: The listener is currently unable to identify the service requested in the connection descriptor " is added as follows, this example uses oracle10g to find the \ c Oracle\product\10.2.0\db_1\network\admin under Lisener.org, change the section after opening to add red
# Listener.ora Network Configuration file:c:\oracle\product\10.2.0\db_1\network\admin\listener.ora# Generated by Oracle configuration Tools. Sid_list_listener = (sid_list = ( Sid_desc = (sid_name = Plsextproc) (oracle_home = C:\oracle\product\ 10.2.0\db_1) (Program = extproc) ) (Sid_desc = (global_dbname = ORCL) (oracle_home = c \ oracle\product\10.2.0\db_1) (sid_name = ORCL) ) ) LISTENER = (description_list = ( DESCRIPTION = (address = (PROTOCOL = IPC) (KEY = EXTPROC1)) (address = (PROTOCOL = TCP) (HOST = 192.168.121.130) (P ORT = 1521))))
3. Connection string:
String connstr = "Data source= (description= (address= (protocol=tcp) (host=192.168.121.130) (port=1521)) (CONNECT_DATA= (SERVICE_NAME=ORCL))); Persist Security info=true; User Id=scott; Password=tiger; ";
where Oracle database server ip:192.168.121.130
Servicename:orcl
User name: Scott
Password: Tiger
4. View the instance name of Oracle, log in with SYS as DBA, execute the statement select name from V$database;
5,c# Code
Using system;using system.collections.generic;using system.configuration;using Oracle.ManagedDataAccess.Client; Using system.linq;using System.text;namespace adosample{class Program {static void Main (string[] args) {String connstr = "Data source=" (Description= (address= (protocol=tcp) (host=192.168.121.130) (port=15 )) (Connect_data= (SERVICE_NAME=ORCL))); Persist Security info=true; User Id=scott; Password=tiger; "; using (OracleConnection conn = new OracleConnection (connstr)) {String sql = "SELECT * FROM Dept "; Conn. Open (); using (oraclecommand cmd = new OracleCommand (SQL, conn)) {using (OracleDataReader Datar Eader = cmd. ExecuteReader ()) {while (Datareader.read ()) { String obj = (string) datareader[1]; Console.WriteLine (obj); }}}} Console.readkey (); } }}
Ado. NET Connect Oracle