2017-04-14 77 views
1

在我的asp.net web-api中,從下面的代碼片段中得到錯誤爲「ORA-00936:missing expression」。我曾嘗試過很多解決方案,但我沒有克服這個錯誤。而且我想知道如何動態綁定多個參數。我使用oracle作爲我的後端和精巧的我的ORM。小巧的sql查詢動態參數

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

     } 
+1

是一個有效的sql語句嗎?你有沒有嘗試在數據庫上運行sql語句? – ChaiNavawongse

回答

3

最後我解決了我的代碼變化不大,我的問題。這就是@Empid變成:Empid,因爲oracle數據庫以這種方式支持動態參數。而對於我的第二個問題是如何與多個動態參數,我使用dappers DynamicParameters類處理,如下圖所示,

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

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

而且我們可以在短小精悍的使用如下圖所示的代碼snippe, 參數查詢是示例sql查詢。

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

來到這裏說「嘗試:EmpId」,但你已經到了那裏。 –

0

所以,你有一個邏輯問題就在這裏,因爲actinact = 0,cancel_idno不爲空列出了兩次

注意,這可能是錯誤

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 

:這很容易當代碼被很好地格式化時。


你說要兩個參數只是添加然後在新的對象

new { EmpId = empId } 

成爲

new { EmpId = empId, 
     newparam = newvalue }) 

,然後在查詢中使用@newparam。

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

+0

我嘗試刪除該行,但仍面臨同樣的問題。 –