[C#] Connessione ORACLE

Prima di iniziare bisogna configurare l’ambiente!

1) Scaricate Oracle Client se non lo avete già….
2) In Visual Studio 2005, importare nel progetto la libreria System.Data.Oracle
3) Aprite il file “\Oracle\product\11.1.0\client_1\network\admin\TNSNAMES.ORA” e riempitelo con la vostra configurazione.

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]NAME =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.38.67)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = service.name.com)
)
)[/code]

Utilizzare una connection string tipo questa, con i vostri parametri:

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]"Data Source=DBNAME;Password=PWD;User ID=USER;Unicode=True";[/code]

Iniziamo con il codice C#…

Apertura e chiusura database:

private string m_oraConnString;
private OracleConnection m_oraConnection;
private OracleCommand m_oraCommand;
private OracleDataReader m_oraDataReader;
private string m_oraDbConnection;

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]/*** apre la connessione al db ***/
private void openDb(bool isProcedure) {
try {
m_oraConnection = new OracleConnection(m_oraConnString);
m_oraDbConnection = m_oraConnection.DataSource;
m_oraCommand = new OracleCommand();

/* TRUE se è necessario chiamare una procedura anzichè una query */
if (!isProcedure)
m_oraCommand.CommandType = CommandType.Text;
else
m_oraCommand.CommandType = CommandType.StoredProcedure;

m_oraCommand.Connection = m_oraConnection;
m_oraConnection.Open();
} catch { }
}[/code]

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]/*** chiude la connessione ***/
private void closeDb() {
try {
m_oraConnection.Close();
m_oraConnection = null;
} catch { }
}[/code]

In caso di chiamata ad una procedura Oracle, è possibile passare o ricevere parametri.

Esempio:

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]/*** procedura ***/
m_oraCommand.CommandText = "TUA PROCEDURA";
/*** parametri input ***/
m_oraCommand.Parameters.Add(
new OracleParameter("dataelab", OracleType.VarChar, 255)).Value = myString;
/*** parametri output ***/
m_oraCommand.Parameters.Add(
new OracleParameter("out_esito", OracleType.Number)).Direction = ParameterDirection.Output;m_oraCommand.ExecuteNonQuery();[/code]

E’ possibile ovviamente ricevere risultati da una query:

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]m_oraCommand.CommandText = "SELECT * FROM tabella";
m_oraDataReader = m_oraCommand.ExecuteReader();
while (m_oraDataReader.Read())
{
Console.WriteLine(string.Format("{0} – {1}",
m_oraDataReader["A"], m_oraDataReader["B"]));
}[/code]

In genere, quando si eseguono query di truncate delete o comunque query di aggiornamento, è consigliabile se non necessario eseguire una commit finale per scrivere le modifiche.

[code lang=”csharp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]m_oraCommand.CommandText = "commit";
m_oraDataReader = m_oraCommand.ExecuteReader();[/code]

Il codice è testato sulla versione 11.1.0.6.0 di ORACLE.
Scaricabile dal sito di Oracle (Download!).

enjoy!

[ref -> albertopasca.it]

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.