Skip to content

Instantly share code, notes, and snippets.

@TristanOrta
Created May 17, 2023 16:10
Show Gist options
  • Save TristanOrta/2c1edfecac94e2fce79848cc81265371 to your computer and use it in GitHub Desktop.
Save TristanOrta/2c1edfecac94e2fce79848cc81265371 to your computer and use it in GitHub Desktop.
This is an example of a C# connection to an Oracle database.
// This class receives as parameters a sql query and a field which brings us the result of the query and saves it in a string
//
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Text;
namespace ssh_CShP
{
class ConexionOra
{
public string conexion(string sql, string tabla)
{
string server = "//127.0.0.1:1525/MyDB.WORLD";
string user = "MyUser";
string password = "MyPass";
string sqlconn = $"Data Source={server}; Password={password}; USER ID = {user};";
string result = null;
try
{
OracleConnection conexion = new OracleConnection(sqlconn);
conexion.Open();
OracleCommand comando = new OracleCommand(sql, conexion);
OracleDataReader lector = comando.ExecuteReader();
if (lector.Read())
{
result = Convert.ToString(lector[tabla]);
}
conexion.Close();
}
catch (Exception e)
{
// Console.WriteLine(e);
result = Convert.ToString(e);
conexion.Close();
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment