Csharp/C Sharp/Database ADO.net/Oracle
Connect to an Oracle server
using System;
using System.Data;
using System.Data.OracleClient;
class OracleConnect
{
static void Main()
{
string connString = @"server = myServer;uid = scott;password = tiger;";
OracleConnection conn = new OracleConnection(connString);
try
{
conn.Open();
Console.WriteLine("Connection opened.");
Console.WriteLine("Connection Properties:");
Console.WriteLine("\tConnection String: {0}", conn.ConnectionString);
Console.WriteLine("\tServerVersion: {0}", conn.ServerVersion);
Console.WriteLine("\tState: {0}", conn.State);
} catch (OracleException e) {
Console.WriteLine("Error: " + e);
} finally {
conn.Close();
Console.WriteLine("Connection closed.");
}
}
}
How to use an OleDbConnection object to connect to an Oracle database
using System;
using System.Data;
using System.Data.OleDb;
class OleDbConnectionOracle
{
public static void Main()
{
string connectionString = "provider=MSDAORA;data source=ORCL;user id=SCOTT;password=TIGER";
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
myOleDbCommand.rumandText = "SELECT empno, ename, sal FROM emp WHERE empno = 7369";
myOleDbConnection.Open();
OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
myOleDbDataReader.Read();
Console.WriteLine("myOleDbDataReader[\" empno\"] = " + myOleDbDataReader["empno"]);
Console.WriteLine("myOleDbDataReader[\" ename\"] = " + myOleDbDataReader["ename"]);
Console.WriteLine("myOleDbDataReader[\" sal\"] = " + myOleDbDataReader["sal"]);
myOleDbDataReader.Close();
myOleDbConnection.Close();
}
}
Oracle connection string for C#
using System;
using System.Data;
using System.Data.OleDb;
class OleDbConnectionOracle
{
public static void Main()
{
string connectionString = "provider=MSDAORA;data source=ORCL;user id=SCOTT;password=TIGER";
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
myOleDbCommand.rumandText = "SELECT empno, ename, sal FROM emp WHERE empno = 7369";
myOleDbConnection.Open();
OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
myOleDbDataReader.Read();
Console.WriteLine("myOleDbDataReader[\" empno\"] = " + myOleDbDataReader["empno"]);
Console.WriteLine("myOleDbDataReader[\" ename\"] = " + myOleDbDataReader["ename"]);
Console.WriteLine("myOleDbDataReader[\" sal\"] = " + myOleDbDataReader["sal"]);
myOleDbDataReader.Close();
myOleDbConnection.Close();
}
}