从C#使用此查询时索引超出范围异常

时间:2012-05-31 04:20:42

标签: c# sql-server

我在SQL查询中使用计算。如何在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 个答案:

答案 0 :(得分: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

注意:这些只是黑暗中的箭头!

答案 1 :(得分:0)

尝试将其与索引而不是列名一起使用。

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

        parentCompanyReader.Close();

我假设计算字段返回一个整数

并且还要确保你使用using语句,以便在出现错误时进行连接,然后关闭并处理连接