Created
November 6, 2015 14:59
-
-
Save Entalyan/aedd85b5ca7a9f3731da to your computer and use it in GitHub Desktop.
Example code for using ODP.NET to connect 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
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