We start off by creating a project of .net core 3.1. Go to the NuGet Manager and try downloading and installing the following packages,
- Dapper [ Latest stable version ]
- Oracle.ManagedDataAccess.Core [ Latest Stable Version ]
The installed arena of Nuget package Manager should like something like this,

Lets begin by creating a static class named Connection Manager. Here first we have to create our connection string which would look something like this.
- private static string connString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host id)(PORT=specified port number)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service name)));User Id = user Id; Password = your password; ";
- public static IDbConnection GetConnection()
- {
- var conn = new OracleConnection(connString);
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
- return conn;
- }
- public static void CloseConnection(IDbConnection conn)
- {
- if (conn.State == ConnectionState.Open || conn.State == ConnectionState.Broken)
- {
- conn.Close();
- }
- }
So finally our ConnectionManager Class should look something like this.
- using Oracle.ManagedDataAccess.Client;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading.Tasks;
- namespace CoreWithOracleDBDapperAsORM.Utility
- {
- public static class ConnectionManager
- {
- private static string connString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host id)(PORT=specified port number)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service name)));User Id = user Id; Password = your password; ";
- public static IDbConnection GetConnection()
- {
- var conn = new OracleConnection(connString);
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
- return conn;
- }
- public static void CloseConnection(IDbConnection conn)
- {
- if (conn.State == ConnectionState.Open || conn.State == ConnectionState.Broken)
- {
- conn.Close();
- }
- }
- }
- }
- public class Student
- {
- public int RollNumber { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- }
Now let's build our querybuilder who will execute all our query. For starters, we will create 3 functions which would be used for different purposes.
Firstly let's create the GetList method which should take an empty object to return its type.
But first we need to create a generic method which would build our query based on the object that had been sent. Although we need to perform an additional task which is to create a method which would be returning the pascal form of the property of the object as in the database in oracle, the norm is to follow the Pascal case but in C# we usually use the camelCase to get our job done. Another thing is we need to create a method named GetColumnList which should take an object and return all of the properties as strings converted to the camel case.
- private static string GetColumnList(T entity)
- {
- string selectedColumns = "Select ";
- foreach (var prop in entity.GetType().GetProperties())
- {
- if (!prop.Name.Contains("_"))
- {
- selectedColumns = selectedColumns + ConvertToPascalCase(prop.Name) + " AS " + prop.Name + ",";
- }
- }
- return selectedColumns + " From "+ ConvertToPascalCase(entity.GetType().Name);
- }
- private static string ConvertToPascalCase(string str)
- {
- return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToLower();
- }
Now as we have the supporting methods done, let's get back to our GetList Method.
- public static IEnumerable<T> GetList(T entity)
- {
- IDbConnection connection = ConnectionManager.GetConnection();
- var result = connection.Query<T>(GetColumnList(entity));
- ConnectionManager.CloseConnection(connection);
- return result;
- }
- public static T SingleOrDefault(T entity)
- {
- IDbConnection connection = ConnectionManager.GetConnection();
- var result = connection.QueryFirstOrDefault<T>(GetColumnList(entity));
- ConnectionManager.CloseConnection(connection);
- return result;
- }
- public static int? ExecuteAction(string query)
- {
- IDbConnection connection = ConnectionManager.GetConnection();
- var result = connection.Execute(query);
- ConnectionManager.CloseConnection(connection);
- return result;
- }
Now let's look at the whole picture,
- using Dapper;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading.Tasks;
- namespace CoreWithOracleDBDapperAsORM.Utility
- {
- public static class QueryBuilder<T>
- {
- public static IEnumerable<T> GetList(T entity)
- {
- IDbConnection connection = ConnectionManager.GetConnection();
- var result = connection.Query<T>(GetColumnList(entity));
- ConnectionManager.CloseConnection(connection);
- return result;
- }
- public static T SingleOrDefault(T entity)
- {
- IDbConnection connection = ConnectionManager.GetConnection();
- var result = connection.QueryFirstOrDefault<T>(GetColumnList(entity));
- ConnectionManager.CloseConnection(connection);
- return result;
- }
- public static int? ExecuteAction(string query)
- {
- IDbConnection connection = ConnectionManager.GetConnection();
- var result = connection.Execute(query);
- ConnectionManager.CloseConnection(connection);
- return result;
- }
- private static string GetColumnList(T entity)
- {
- string selectedColumns = "Select ";
- foreach (var prop in entity.GetType().GetProperties())
- {
- if (!prop.Name.Contains("_"))
- {
- selectedColumns = selectedColumns + ConvertToPascalCase(prop.Name) + " AS " + prop.Name + ",";
- }
- }
- return selectedColumns + " From " + ConvertToPascalCase(entity.GetType().Name);
- }
- private static string ConvertToPascalCase(string str)
- {
- return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToLower();
- }
- }
- }
Now let's get back to our controller.
- using CoreWithOracleDBDapperAsORM.Model;
- using CoreWithOracleDBDapperAsORM.Utility;
- using Microsoft.AspNetCore.Mvc;
- namespace CoreWithOracleDBDapperAsORM.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class StudentController : ControllerBase
- {
- [HttpGet]
- [Route("GetStudentList")]
- public object GetStudentList()
- {
- return QueryBuilder<Student>.GetList(new Student());
- }
- }
- }
THANK YOU.

Matina MaharjanPosted Sep 22, 2021, 10:11 AM
This article has helped me a lot in my tough time. I was facing issues as "ExecuteReader requires an open and available Connection. The connection's current state is open.", "ExecuteReader requires an open and available Connection. The connection's current state is closed.", "ExecuteReader requires an open and available Connection. The connection's current state is connecting.", "Invalid operation. The Connection is closed.". Now, with the help of your article, the connection issues in dapper is solved. Thank you so much sir for posting.
Nguyen Tu KietPosted Jul 16, 2020, 10:07 AM
Thank you so much for the useful article. It helps me a lot
Sourav Kumar DasPosted Jan 25, 2020, 12:28 PM
Informative useful article on .Net Core Sir.