Install-Package Dapper -Version 1.50.5
Install-Package Oracle.ManagedDataAccess.Core -Version 2.12.0-beta2
appsettings.json
"ConnectionStrings": {
"EmployeeConnection": "data source=xxx;password=xxx;user id=xxx;Incr Pool Size=5;Decr Pool Size=2;"
}
public class HomeController : Controller
{
IConfiguration configuration;
public HomeController( IConfiguration _configuration)
{
configuration = _configuration;
}
public IActionResult Index()
{
return Ok(GetAppRoot());
}
public string GetConnection()
{
var connectionString = configuration.GetSection("ConnectionStrings").GetSection("EmployeeConnection").Value;
return connectionString;
}
public string GetAppRoot()
{
string querySql = @" select id
from user where id=1";
return GetSingleString(GetConnection(), querySql);
}
public string GetSingleString(string conn, string querySql)
{
using (var Conn = CreateConnection(conn))
{
return Conn.Query<string>(querySql).FirstOrDefault();
}
}
public static IDbConnection CreateConnection(string connString)
{
IDbConnection conn = null;
try
{
conn = new Oracle.ManagedDataAccess.Client.OracleConnection(connString);
conn.Open();
}
catch (Exception ex)
{
throw new Exception("Database can not open.", ex);
}
return conn;
}
}