This tutorial provides a step-by-step guide on using Dapper for efficient data operations in .NET applications. With dotConnect for Oracle, we will create Dapper models, develop a console application, and implement CRUD operations using a robust connection class for smooth database interactions.

Why dotConnect for Oracle?

ORM support provided

Advanced ORM support

Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other technologies for efficient data management.

Full ADO.NET compliance

Full ADO.NET compliance

Conforms to the latest ADO.NET standards and innovations for seamless integration with .NET applications.

Support for Oracle-specific data types

Oracle-specific data types

Offers many Oracle-specific features and fully supports all unique data types for accurate and complete data representation.

Secure connection ensured

Secure connection options

Provides robust security with support for SSL/SSH connections, connecting via proxy servers, embedded servers, and HTTP tunneling.

Integration with popular IDEs

IDE integration

Features native integration with Visual Studio and complete design-time support for accelerated development.

Priority support provided

Priority support & frequent updates

Includes priority support, detailed documentation, and regular updates for continuous improvement.

Download and activate dotConnect for Oracle

You can start using dotConnect for Oracle immediately with a 30-day free trial. Choose one of the following installation options:

30-day free trial version

dotnet add package Devart.Data.Oracle
Install-Package Devart.Data.Oracle

You can install the driver by using the Windows installer.

After you receive the license key, add it to your connection strings to connect to the data source.

Start using dotConnect for Oracle in your project today with a free trial

Select Oracle database objects

Using the built-in Data Explorer, first, configure the connection to Oracle and then select the objects you want to use in your project.

1. In Visual Studio, click Tools on the menu bar and select Connect to Database from the drop-down list.

Connect to Database

2. In the Add Connection window, choose the Oracle data source, fill in the connection details for your Oracle database, and click OK to proceed.

Add connection details

3. In Server Explorer, select the tables and fields you want to use. In our tutorial, we will use the EMPLOYEE table.

Selected Employee table

Connect to Oracle and retrieve data

You can now create the application that connects to the Oracle database and displays data from the EMPLOYEE table using Dapper.

Create Employee.cs (model class)

This class maps to the Employee table.


public class Employees
{
    public int Employee_Id { get; set; }
    public string First_Name { get; set; }
    public string Last_Name { get; set; }
    public string Email { get; set; }
    public DateTime Hire_Date { get; set; }
    public int Salary { get; set; }
}

Create DatabaseConnection.cs (connection strings)

This class keeps connection details separate for better maintainability.

namespace DapperOracleExample{
    public static class DatabaseConnection{
        public static string ConnectionString="Direct=True;Host=127.0.0.1;Service Name=OracleDb;User ID=TestUser;Password=TestPassword;License Key=**********";
    }
}

Direct mode options properties

Property Meaning
Direct Specifies whether to use direct mode
Server or Host States the IP address or hostname of the Oracle server
Port Indicates the port number for the Oracle server
ServiceName Determines the service name for the database instance
Sid Specifies the Oracle System Identifier (SID) for the database instance
UserId States the Oracle database username
Password Defines the password associated with the Oracle database username
License key Specify your license key in this parameter. This is required only when using .NET Standard compatible assemblies

In your Program.cs file, add the connection string to the Oracle database. It should be structured like this:

using Dapper;
using Devart.Data.Oracle;

namespace DapperOracleExample{
    class Program{

        static async Task Main(string[] args){
            try{
                using(var connection=new OracleConnection(DatabaseConnection.ConnectionString)){
                    // Open the database connection 
                    connection.Open();

                    // Define the SQL query to retrieve data from the EMPLOYEE table 
                    string sql="SELECT * FROM EMPLOYEE";

                    // Execute the query and map the results to a list of dynamic objects 
                    var employees=connection.Query(sql).ToList();

                    // Display the retrieved data 
                    foreach(var employee in employees){
                        Console.WriteLine(employee);
                    }
                }
            }
            catch(Exception ex){
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }
}
Update the connection string
To update the connection string, simply replace the placeholders in the connection string with your actual Oracle database credentials.

Run the application

To build and run your application, press F5 or select Start from the menu in Visual Studio.

Build and run application

Mapping to strongly-typed objects

Mapping query results to strongly-typed objects improves code readability and maintainability. You can follow the example code below to map query results to strongly-typed objects using Dapper.

public class Employee{
    public int Id{
        get;
        set;
    }
    public string Name{
        get;
        set;
    }
    public int DepartmentId{
        get;
        set;
    }
}

using(var connection=new OracleConnection(connectionString)){
    connection.Open();
    string sql="SELECT * FROM EMPLOYEE";
    var employees=connection.Query<Employee>(sql).ToList();

    foreach(var employee in employees){
        Console.WriteLine($"{employee.Id}: {employee.Name} (Department ID: {employee.DepartmentId})");
    }
}

As a result of mapping, the query results are transformed into strongly-typed objects, allowing you to work with them more effectively in your C# application.

Mapping result

Insert new records using Dapper

Let's examine how to insert 10 new rows into the EMPLOYEE table using Dapper in C#. This example assumes that your EMPLOYEE table has two columns: NAME and POSITION.

using Dapper;
using Devart.Data.Oracle;

namespace DapperOracleExample
{
    class Program
    {

        static async Task Main(string[] args)
        {
            try
            {
                // Sample data to insert with new names and positions 
                var employees = new[]{
                    new{
                        NAME = "Karen White", POSITION = "Project Manager"
                    },
                    new{
                        NAME = "Liam Green", POSITION = "Software Engineer"
                    },
                    new{
                        NAME = "Mia Black", POSITION = "UX Designer"
                    },
                    new{
                        NAME = "Noah Blue", POSITION = "Data Scientist"
                    },
                    new{
                        NAME = "Olivia Red", POSITION = "QA Engineer"
                    },
                    new{
                        NAME = "Peter Yellow", POSITION = "Technical Support"
                    },
                    new{
                        NAME = "Quinn Pink", POSITION = "Recruiter"
                    },
                    new{
                        NAME = "Rachel Purple", POSITION = "Marketing Specialist"
                    },
                    new{
                        NAME = "Samuel Orange", POSITION = "Sales Representative"
                    },
                    new{
                        NAME = "Tina Grey", POSITION = "Business Analyst"
                    }
                };

                using(var connection = new OracleConnection(DatabaseConnection.ConnectionString))
                {
                    connection.Open();

                    // Define the SQL insert statement without the ID column 
                    string insertSql = "INSERT INTO EMPLOYEE (NAME, POSITION) VALUES (:NAME, :POSITION)";

                    // Execute the insert operation for each employee 
                    foreach(var employee in employees)
                    {
                        connection.Execute(insertSql, employee);
                    }

                    Console.WriteLine("Data inserted successfully.");

                    // Define the SQL select statement to retrieve all rows 
                    string selectSql = "SELECT * FROM EMPLOYEE";

                    // Execute the select query and map the results to a list of dynamic objects 
                    var allEmployees = connection.Query(selectSql).ToList();

                    // Display the retrieved data 
                    Console.WriteLine("\nAll rows in the EMPLOYEE table:");

                    foreach(var employee in allEmployees)
                    {
                        Console.WriteLine($"ID: {employee.ID}, Name: {employee.NAME}, Position: {employee.POSITION}");
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }
}

After running the program, you should see the following output in the console:

Data inserted successfully

Delete Oracle data using Dapper

To delete rows from the EMPLOYEE table where the ID is between 71 and 80, you can use the following example with Dapper:

1. In the ActorController class, add the following method:

using Dapper;
using Devart.Data.Oracle;

namespace DapperOracleExample
{
    class Program
    {

        static async Task Main(string[] args)
        {
            try
            {
                using (var connection = new OracleConnection(DatabaseConnection.ConnectionString))
                {
                    await connection.OpenAsync();

                    string deleteSql = "DELETE FROM employees WHERE employee_id = :Id";
                    connection.Execute(deleteSql, new { Id = 63 });
                    Console.WriteLine("Employee deleted successfully.\n");

                    Console.WriteLine("Fetching all employees:\n");

                    var query = "SELECT employee_id AS Employee_Id, first_name AS First_Name, last_name AS Last_Name, email AS Email, hire_date AS Hire_Date, salary AS Salary FROM employees";
                    var employees = await connection.QueryAsync<Employees>(query);

                    foreach (var employee in employees)
                    {
                        Console.WriteLine($"ID: {employee.Employee_Id}, Name: {employee.First_Name} {employee.Last_Name}, Email: {employee.Email}, Hire Date: {employee.Hire_Date}, Salary: {employee.Salary}");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }
}

The retrieved data is then displayed in the console, showing the id, name, and position of each remaining employee.

JSON object added to the body of a PUT request for the UpdateActor endpoint

Video tutorial: Connect Oracle using Dapper ORM

Conclusion

In this tutorial, we explored how integrating Dapper with dotConnect for Oracle streamlines data operations in .NET applications. The powerful features of dotConnect, combined with Dapper’s lightweight efficiency, provide a fast and flexible approach to managing database interactions. With these tools, you can confidently build scalable, maintainable, and high-performance .NET solutions that connect effortlessly to your database.

FAQ

How do you install and activate dotConnect for Oracle in a .NET project?
Install dotConnect for Oracle via the EXE installer or by adding the Devart.Data.Oracle NuGet package to your project, then obtain your personal activation key from your Devart Customer Portal and include it in the connection string via the License Key parameter for a working connection.
How do you create a connection to Oracle using dotConnect in C#?
Define a connection string that includes host, user ID, password, database, and the License Key value, then create an OracleConnection instance with this string and call Open() for it inside a try-catch block to test and handle connection errors.
How do you enable encryption for secure Oracle connections with dotConnect?
Use Oracle Native Network Encryption by configuring encryption in the sqlnet.ora file on both client and server sides. No special parameters are required in the connection string.
Can you connect to Oracle using Entity Framework Core and dotConnect?
Yes, you can either use Entity Developer to visually create an EF Core model from the database or run Scaffold-DbContext with a dotConnect connection string (including License Key) to generate the DbContext and entity classes.
Is it possible to connect to Oracle using Visual Studio Server Explorer with dotConnect?
Yes, it is possible. All you need to do is navigate to Visual Studio Server Explorer, add a new Data Connection, choose dotConnect for Oracle as the data provider, enter your credentials, test the connection, and then browse Oracle data directly in Visual Studio.

Dereck Mushingairi

I'm a technical content writer who loves turning complex topics — think SQL, connectors, and backend chaos–into content that actually makes sense (and maybe even makes you smile). I write for devs, data folks, and curious minds who want less fluff and more clarity. When I'm not wrangling words, you'll find me dancing salsa, or hopping between cities.

Try the 30-day trial of the full product. No limits. No card required. Start free trial