Skip to content

Instantly share code, notes, and snippets.

@Entalyan
Created November 6, 2015 14:59
Show Gist options
  • Save Entalyan/aedd85b5ca7a9f3731da to your computer and use it in GitHub Desktop.
Save Entalyan/aedd85b5ca7a9f3731da to your computer and use it in GitHub Desktop.
Example code for using ODP.NET to connect to an Oracle database.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Oracle.DataAccess.Client;
namespace CSharp_Console
{
static class Program
{
static void Main(string[] args)
{
// Properties.Settings.Default.OracleString contains the connection string, including credentials.
// This is not good practice, but for this example I'm making an exception.
using (var dbConnection = new OracleConnection(Properties.Settings.Default.OracleString))
{
try
{
dbConnection.Open();
if (dbConnection.State == ConnectionState.Open)
{
Console.WriteLine("Connected to " + dbConnection.HostName + " running Oracle " + dbConnection.ServerVersion);
}
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong: " + ex.Message);
}
finally
{
dbConnection.Close();
}
}
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment