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

本文关键字:索引 范围 异常 查询 | 更新日期: 2023-09-27 18:35:45

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

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

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

问题仅出在计算字段上。上面的评论是绝对正确的。可能是您的父公司阅读器类有错误。同时,您可以尝试一些方法来确认错误。

将 eff 列移到中间的某个位置。可能是您的父公司读者无法仅处理有限数量的列。

还有这里的"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 语句,以便在出现错误的情况下,连接将被关闭并处理