Created
May 17, 2023 16:10
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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