在Dapper中处理Oracle数据库连接

时间:2016-07-21 17:43:00

标签: asp.net asp.net-mvc asp.net-web-api dapper

我正在尝试连接到Oracle数据库并尝试执行查询。

以下是我的模型类

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类是

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; }
}
}

现在尝试使用Dapper创建Controller,现在问题出现在它尝试连接到SQL数据库的大多数论坛中。我正在尝试访问Oracle DB并以JSON格式返回结果。如果我给出

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();
  }
 }
}

它抛出一个错误,说dbconn.Query不在上下文中,我也试过TestAPIContext.Init,即使抛出错误。任何人都可以建议我如何处理与Dapper的Oracle连接。我是ASP.NET和创建服务的新手。真的被困了,非常感谢任何帮助。

2 个答案:

答案 0 :(得分:5)

您没有传递SQL。此外,不需要显式关闭。您可以将代码包装在一个使用中,因为SqlConnection.Dispose()会调用SqlConnection.Close()。

也许这是一个错字,但“回归”应该是“回归”。应该从app.config和硬编码中读取Connectionstring,我还考虑将'id'作为参数。

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);
}

答案 1 :(得分:0)

我可以使用以下解决方案来解决这个问题:

1。。从nuget安装Oracle.ManagedDataAccess.Core软件包。
2。如下编写ConnectionString:

Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=0.0.0.0)(PORT=1521))(CONNECT_DATA=(SID=sidName))); User Id=*****; Password=******;

PORT=1521 => Oracle的默认端口为1521,您可以使用Oracle服务器端口进行更改
0.0.0.0 => Oracle服务器IP或名称
sidName => oracle服务器的SidName

3。 C#代码段:

protected IDbConnection GetChargingOracleDbConnection
{
    get
    {
        var oracleConnection = new OracleConnection("OracleConnectionString");
        oracleConnection.Open();
        return oracleConnection;
    }
}

var query = "SELECT Id, Name FROM Service";
using (var dbConnection = GetChargingOracleDbConnection)
{
    return dbConnection.Query<MyModel>(query).ToList();
}