收集参数用小巧玲珑查询oracle

时间:2016-07-22 17:56:23

标签: c# oracle dapper

在WEB API服务中传递参数数组时遇到问题

public class SampleController : ApiController
{
   public string Getdetails([FromUri] int[] id) 
   {
    var inconditions = id.Distinct().ToArray();
    using (var dbConn = new OracleConnection("DATA SOURCE=h;PASSWORD=C;PERSIST SECURITY INFO=True;USER ID=T"))
    {
        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  IN (:p)";
        var queryResult = dbConn.Query<SamModel>(strQuery, new { p = inconditions });
        return JsonConvert.SerializeObject(queryResult);
    }
}

现在,在调用API为http://localhost:35432/api/Sample?id=1&id=83时,它会在var queryResult = dbConn.Query(strQuery);

上引发错误
  

{&#34; ORA-00907:错过右括号&#34;}

enter image description here

检查了所有括号,一切看起来都不错

4 个答案:

答案 0 :(得分:8)

无需将集合参数包装在括号中。 Dapper不仅仅是查找和替换,而且智能应该自己处理集合参数。试试这个:

... WHERE REC_USER IN :p

查看List Support部分。

(:p)案例中,生成了以下类型的查询:

... WHERE REC_USER IN ((1,2,3))

答案 1 :(得分:1)

@lorond用他的评论和answer做对了。

他提供的链接确认您的代码可以重构为

public class SampleController : ApiController {
   public string Getdetails([FromUri] int[] id) {
    var inconditions = id.Distinct().ToArray();
    using (var dbConn = new OracleConnection("DATA SOURCE=h;PASSWORD=C;PERSIST SECURITY INFO=True;USER ID=T")) {
        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 IN @p";
        var queryResult = dbConn.Query<SamModel>(strQuery, new { p = inconditions });
        return JsonConvert.SerializeObject(queryResult);
    }
}

答案 2 :(得分:0)

以下代码有效,

var inconditions = id.Distinct().ToArray();
var srtcon = string.Join(",",inconditions);
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  IN ("+srtcon+")";
var queryResult = dbConn.Query<SamModel>(strQuery);
return JsonConvert.SerializeObject(queryResult); 

答案 3 :(得分:-1)

尝试用逗号分隔参数。然后你可以在你的代码中拆分它们。

http://localhost:35432/api/Sample?id=1,83