Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle Oracle Database Connection in Dapper

I am trying to connect to the Oracle Database and trying to execute a query.

So below is my Model Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace TestAPI.Models
{
public class TestAPIModel
{
    [Key]
    public int PRIO_CATEGORY_ID { get; set; }
    public int LANG_ID { get; set; }
    public System.DateTime REC_DATE { get; set; }
    public int REC_USER { get; set; }
    public Nullable<int> RFCH_ID { get; set; }
    public string DESCR { get; set; }
    public string COL_DESCR { get; set; }
    public string ROW_DESCR { get; set; }
    public string ABBR { get; set; }
}
}

DBContext Class is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace TestAPI.Models
{
public class TestAPIContext: DbContext
{
        public DbSet<TestAPIModel> details { get; set; }
}
}

Now trying to create the Controller with the Dapper, now the issue is in most of the forums it is trying to connect to SQL Database. I am trying to access Oracle DB and return the result in JSON format .So if I give

using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using System.Web.Http.Description;
using TestAPI.Models;
using Dapper;

namespace TestAPI.Controllers
 {
  public class TestAPIModelsController : ApiController
   {
     // GET: api/TestAPIModels
    public IQueryable<TestAPIModel> Getdetails(int id)
    {
      OracleConnection dbConn = new OracleConnection("DATA SOURCE=AX;PASSWORD=CM;PERSIST SECURITY INFO=True;USER ID=AB");
      dbConn.Open();
      var strQuery = @"Select PRIO_CATEGORY_ID as PRIO,LANG_ID as LANG, REC_DATE as REC, REC_USER as RECUSER, DESCR,COL_DESCR AS COL,ROW_DESCR as DROW,ABBR from STCD_PRIO_CATEGORY_DESCR where REC_USER =  " +id;
      retrun dbConn.Query<TestAPIModel>();
      dbConn.Close();
  }
 }
}

It throws an error saying that the dbconn.Query is not in context and I also tried TestAPIContext.Init even that throws error. Can anyone please suggest me how to deal Oracle connection with the Dapper. I am new to ASP.NET and the Creating the services. kind of really stuck, any help is greatly appreciated.

like image 239
trx Avatar asked Dec 19 '22 15:12

trx


1 Answers

You were not passing the SQL. Also, the explicit close is not needed. You can wrap the code in a using as under the hood SqlConnection.Dispose() calls the SqlConnection.Close().

Perhaps this a typo, but "retrun" should be "return". Connectionstring should be read from the app.config vs. hard coded and I'd also consider making 'id' a parameter.

using (var dbConn = new OracleConnection("DATA SOURCE=AX;PASSWORD=CM;PERSIST SECURITY INFO=True;USER ID=AB"))
{
      dbConn.Open();
      var strQuery = @"Select PRIO_CATEGORY_ID as PRIO,LANG_ID as LANG, REC_DATE as REC, REC_USER as RECUSER, DESCR,COL_DESCR AS COL,ROW_DESCR as DROW,ABBR from STCD_PRIO_CATEGORY_DESCR where REC_USER = " +id;
      return dbConn.Query<TestAPIModel>(strQuery);
}
like image 188
William Xifaras Avatar answered Jan 05 '23 10:01

William Xifaras