2012-05-31 94 views
0

我在我的SQL查詢中使用了一個計算。我如何在C#中使用該計算字段?當我嘗試時,我得到一個索引超出範圍的例外。使用C#中的查詢時,索引超出範圍例外

我的查詢是:

Select OwnerCompanyLog.olog_name,inlt_companyid,inlt_childcompid,inlt_effectinterest,inlt_percent,inlt_sharetype,inlt_shares,inlt_childbase,inlt_effdate, 
    ((inlt_percent * inlt_effectinterest)/100)eff 
    from InterestLogTable 
    INNER JOIN OwnerCompanyLog 
    ON 
    InterestLogTable.inlt_childcompid = OwnerCompanyLog.olog_companyid 
    where inlt_companyid=5 
    Order By inlt_childcompid 

我想在我的C#代碼使用inlt_percent * inlt_effectinterest)/100

entity.ParentCompany = new List<Company>(); 
    while (parentCompanyReader.Read()) 
      { 
    ParentCompany.Effect = parentCompanyReader["eff"].ToString(); 
    entity.ParentCompany.Add(ParentCompany); 
      } 

      parentCompanyReader.Close(); 

但我得到上述錯誤。

+2

它不是一個sql錯誤.. problwm似乎是在你的C#數組'parentCompanyReader',...你需要發佈你的C#代碼在這裏 – Kshitij

回答

0

該問題只與計算字段有關。以上評論絕對正確。它可能是你的parentCompanyReader類有錯誤。同時你可以嘗試幾件事來確認錯誤。

將eff列移到中間。這可能是因爲你的parentCompanyReader不能僅處理有限數量的列。

什麼是「eff」在這裏,一列?計算列後也放一個AS。

Select (inlt_percent * inlt_effectinterest)/100)eff AS EFF, OwnerCompanyLog.olog_name,inlt_companyid,inlt_childcompid,inlt_effectinterest,inlt_percent,inlt_sharetype,inlt_shares,inlt_childbase,inlt_effdate 

注意:這些只是箭頭在黑暗中!

0

試着將它與索引而不是列名一起使用。

 while (parentCompanyReader.Read()) 
     { 
     ParentCompany.Effect = Convert.ToInt32(parentCompanyReader[5]); 
     entity.ParentCompany.Add(ParentCompany); 
     } 

     parentCompanyReader.Close(); 

林假設計算的字段返回一個整數

,並確保ü使用using語句,這樣,櫃面,如果有一個錯誤,那麼將關閉連接並設置