2017-04-14 22 views
1

私のasp.net web-apiでは、下のコードスニペットから「ORA-00936:missing expression」というエラーが表示されています。私は多くの解決策を試しましたが、私はこのエラーを克服しませんでした。また、私はどのように私は動的に複数のパラメータをバインドするか知りたいです。私はオラクルを私のバックエンドとして使用しています。SQLクエリ動的パラメータを持つDapper

 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クラスを使用しています複数の動的パラメータを処理する方法である私の2番目の質問、

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

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

ため、コードsnippeの下に示したように、我々はdapperのこれを使用することができ、 パラメータクエリがありますサンプルSQLクエリ。

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

がここに来て「try:EmpId」と言いましたが、あなたは既にそこにいます。 –

0

ですから、actinact = 0とcancel_idnoがnullではない二回

注意がリストされているため、エラー

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 

かもしれないここにロジックの問題があります。これは簡単ですがコードがきれいにフォーマットされていることを発見する。


あなたは2つのパラメータだけ

new { EmpId = empId } 

new { EmpId = empId, 
     newparam = newvalue }) 

となり、その後、クエリで@newparam使う新しいオブジェクトで、その後追加したいと言います。

ドキュメント - >https://msdn.microsoft.com/en-us/library/bb397696.aspx

+0

私はその行を削除してみましたが、私は同じ問題に直面しています。 –

関連する問題