【问题标题】:Dapper with sql query dynamic parametersDapper 带 sql 查询动态参数
【发布时间】:2017-09-10 18:25:28
【问题描述】:

在我的 asp.net web-api 中,从下面的代码 sn-p 中收到错误为“ORA-00936:缺少表达式”。我尝试了很多解决方案,但我没有克服这个错误。而且我还想知道如何动态绑定多个参数。我使用 oracle 作为我的后端,使用 dapper 作为我的 ORM。

        string empId = json.EMPID; //'15RD005'

        var sql = @"Select id_no,SNO,REASON,APPLIEDDATE,Case
                    when LEAVE_TYPE = 0 then 'CL'
                    when LEAVE_TYPE = 1 then 'EL'
                    when LEAVE_TYPE = 2 then 'SL'
                    when LEAVE_TYPE = 3 then 'OFF'
                    when LEAVE_TYPE = 4 then 'OD-OFF'
                    when LEAVE_TYPE = 5 then 'LOP'
                    when LEAVE_TYPE = 6 then 'OPTIONAL' end LEAVE_TYPE,
                to_char(fromdate,'DD-MON-YYYY') f_date, to_char(todate,'DD-MON-YYYY') t_date,
                    Case when fromslot=0 then 'First-Half' when fromslot=1 then 'Second-Half' when fromslot=2 then 'Full-Day' end From_Slot,
                    Case when toslot=0 then 'First-Half' when toslot=1 then 'Second-Half' when toslot=2 then 'Full-Day' end To_Slot,
                applieddays APP_DAYS,
                    case when actinact=0 and cancel_idno is not null then 'Cancelled'
                    when actinact=1 and AUTH_IDNO   is null then 'Pending'
                    when actinact=0 and cancel_idno is not null then 'Rejected'
                    else 'Authorised' end leave_Status
                from Tleaves where to_char(Todate,'mm-yyyy') >= to_char(sysdate-30,'mm-yyyy') and to_char(todate,'mm-yyyy') <=to_char(sysdate,'mm-yyyy')
                 and to_char(Todate,'yyyy')=to_char(sysdate,'yyyy') and id_no like @EmpId Order by sno";



        try
        {
            using (OracleConnection db = new OracleConnection(conString))
            {
                db.Open();

                var pastLeavesReport = new PastLeavesReportDTO();
                //3.Present and last month lev status report
                List<PastLeavesReportInfoDTO> pastLeavesReportInfo = db.Query<PastLeavesReportInfoDTO>(sql, new { EmpId = empId }).ToList();

                pastLeavesReport.EMPID = "";
                pastLeavesReport.LEAVES = pastLeavesReportInfo;


                return Ok(
                 new EmpLeavesActionResponse(ActionStatusCodes.PastLeavesReportDataFound,
                               "",
                               pastLeavesReport));


            }
        }
        catch (Exception exp)
        {

            return Ok(
                 new EmpLeavesActionResponse(ActionStatusCodes.ServerException,
                               exp.Message,
                               null));

        }

【问题讨论】:

  • 这是一个有效的sql语句吗?你试过只在数据库上运行 sql 语句吗?

标签: c# sql oracle asp.net-web-api dapper


【解决方案1】:

所以你这里有一个逻辑问题,可能是错误

case 
  when actinact=0 and cancel_idno is not null then 'Cancelled'
  when actinact=1 and AUTH_IDNO   is null then 'Pending'
  when actinact=0 and cancel_idno is not null then 'Rejected'
  else 'Authorised' 
end leave_Status

因为 actinact=0 和 cancel_idno 不为 null 被列出两次

注意:当代码格式正确时,很容易发现这一点。


你说你想要两个参数,然后在新对象中添加

new { EmpId = empId }

变成

new { EmpId = empId,
      newparam = newvalue })

然后在查询中使用@newparam。

doc -> https://msdn.microsoft.com/en-us/library/bb397696.aspx

【讨论】:

  • 我尝试删除该行,但我仍然面临同样的问题。
【解决方案2】:

最后我解决了我的问题,我的代码几乎没有变化。也就是把@Empid 改成:Empid,因为oracle 数据库是这样支持动态参数的。对于我的第二个问题,即如何处理多个动态参数,我使用的是 dappers DynamicParameters 类,如下所示,

        var parameters = new Dictionary<string, object>();
        parameters.Add("ID", empId);

        DynamicParameters dbParams = new DynamicParameters();
        dbParams.AddDynamicParams(parameters);

我们可以在 dapper 中使用它,如下面的代码片段所示, 参数查询是一个示例sql查询。

          dynamic result = db.Query(query, dbParams);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-30
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多