In order to connect to an Oracle database from .NET, use the Oracle.ManagedDataAccess package, otherwise called ODP.NET. It can be downloaded with NuGet by searching “oracle.”
You can get started easily with defining a connection string. Later, you can put this into App.Config with different Data Sources for Testing and Production environments. Don’t forget the using statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using Oracle.ManagedDataAccess.Client; namespace OracleConnect { class Program { static void Main(string[] args) { string conString = "User Id=HR;Password=HR;" + "Data Source=localhost:1521/xe"; OracleConnection con = new OracleConnection(conString); con.Open(); OracleCommand cmd = con.CreateCommand(); cmd.CommandText = "Select first_name, last_name from employees"; OracleDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader.GetString(0) + "\t" + reader.GetString(1)); } con.Dispose(); Console.ReadLine(); } } } |
1 2 3 4 5 6 7 8 9 10 11 |
... Nandita Sarchand Ismael Sciarra John Seo Sarath Sewall Lindsey Smith William Smith Stephen Stiles Martha Sullivan Patrick Sully >_ |