05 November 2026

Connect C# to Oracle with ODP.NET Core and Named Parameters

  Asp.Net CS By Example       05 November 2026

Many older examples on this blog use System.Data.OracleClient. For a modern .NET console application, this follow-up demonstrates Oracle's ODP.NET Core provider and a parameterized query that does not require an employee sample schema.

Choose the provider for your project

Microsoft marks System.Data.OracleClient as deprecated. Oracle distributes ODP.NET Core through the Oracle.ManagedDataAccess.Core NuGet package. The code namespace remains Oracle.ManagedDataAccess.Client. A classic ASP.NET Web Forms project targeting .NET Framework needs the corresponding compatible managed-driver package and its own migration checks; the Core package is not a blanket replacement for every old project.

1. Create the console project

dotnet new console -n OracleNamedParameters
cd OracleNamedParameters
dotnet add package Oracle.ManagedDataAccess.Core

Use a .NET SDK supported by the package version you select. Review the NuGet dependency information before adding it, and keep the resolved version under source control with your project.

2. Configure your connection outside the source file

Set an environment variable named ORACLE_DEMO_CONNECTION through your development environment or deployment configuration. A typical connection-string shape is:

User Id=YOUR_USER;Password=YOUR_PASSWORD;Data Source=YOUR_HOST:1521/YOUR_SERVICE

Replace every placeholder with your own development database settings. Do not commit a real password or print the complete connection string in logs. The database account only needs the privileges required by your application.

3. Replace Program.cs

using System;
using Oracle.ManagedDataAccess.Client;

string? connectionString =
    Environment.GetEnvironmentVariable("ORACLE_DEMO_CONNECTION");

if (string.IsNullOrWhiteSpace(connectionString))
{
    Console.WriteLine("Configure ORACLE_DEMO_CONNECTION before running.");
    return;
}

using var connection = new OracleConnection(connectionString);
connection.Open();

using var command = connection.CreateCommand();
command.BindByName = true;
command.CommandText =
    "SELECT :p_text AS message, :p_number + 1 AS next_value FROM dual";

// Intentionally add in a different order from the SQL text.
command.Parameters.Add("p_number", OracleDbType.Int32).Value = 41;
command.Parameters.Add("p_text", OracleDbType.Varchar2, 80).Value = "Hello Oracle";

using var reader = command.ExecuteReader();
if (reader.Read())
{
    Console.WriteLine(reader.GetString(0));
    Console.WriteLine(reader.GetDecimal(1));
}

The SQL uses colon-prefixed placeholders. The Parameters collection uses the matching names without a colon. Setting BindByName explicitly makes the relationship easy to see, even though the collection order differs from the SQL order. Use OracleDbType for this provider; SqlDbType belongs to SQL Server examples.

4. Run and check the output

dotnet run

With a valid connection, this query should print Hello Oracle followed by 42. DUAL keeps the example independent of any HR or EMP table. This database example was reviewed against Oracle documentation; it was not run against your database.

Adapt the pattern to a lookup

command.CommandText =
    "SELECT employee_name FROM app_employee WHERE employee_id = :id";
command.Parameters.Clear();
command.Parameters.Add("id", OracleDbType.Int32).Value = employeeId;

This adaptation assumes your own app_employee table and a validated integer employeeId variable. A parameter represents a value; it does not replace a table name, column name, or sort direction. Use a small allowlist when users can select a sort column.

Common connection checks

  • Verify the database host, listener port and service name with your DBA.
  • Check that the selected package supports the project's target framework.
  • Handle exceptions at an application boundary and show users a general failure message.
  • Dispose the reader, command and connection after each logical operation, as shown by the using declarations.

Continue reading

Connect to Oracle DB Using ADO.NET and ADO.NET Parameters Queries explain the older ADO.NET flow.

References

 

logoblog

Thanks for reading Connect C# to Oracle with ODP.NET Core and Named Parameters

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.