繁体   English   中英

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

[英]Index out of range exception when using this query from C#

我在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();

但是我得到了上面的错误。

问题仅在于计算字段。 以上评论绝对正确。 可能是您的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

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

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

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

        parentCompanyReader.Close();

我假设计算字段返回整数

并确保您使用using语句,以便万一发生错误,则连接将被关闭并处理

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM