【问题标题】:Reading multiple rows of data using sqldatareader使用 sqldatareader 读取多行数据
【发布时间】:2012-02-02 17:59:06
【问题描述】:

我的sql语句如下:

SELECT * FROM ViewSectorInvestments WHERE AccountNumber = @AccountNumber

ViewSectorInvestments 中的字段:

AccountNumber
SectorName
AmountInvested

我正在尝试根据总投资计算每个部门的 AmountInvested。 所以公式将是:AmountInvested/TotalInvestments * 100

我的代码如下:

    string DMConnectionString = ConfigurationManager.ConnectionStrings["DMConnectionString"].ConnectionString;
    SqlConnection DMConnection = new SqlConnection(DMConnectionString);
    DMConnection.ConnectionString = DMConnectionString;

    string DMCommandText = "SELECT Name,RiskProfile,AccountNumber,TotalInvestments FROM ViewClientDetails WHERE AccountNumber = @AccountNumber; SELECT * FROM ViewSectorInvestments WHERE AccountNumber = @AccountNumber ;SELECT * FROM ViewStockTypeInvestments WHERE AccountNumber = @AccountNumber  ";
    SqlCommand DMCommand = new SqlCommand(DMCommandText, DMConnection);
    DMCommand.Parameters.AddWithValue("@AccountNumber", lb_AcctNum.Text);
    DMConnection.Open();

    SqlDataReader DMReader = DMCommand.ExecuteReader();

    ArrayList SectorArray = new ArrayList();
    ArrayList StockTypeArray = new ArrayList();

    while (DMReader.Read())
    {
        CustName.Text = DMReader["Name"].ToString();
        lb_Risk.Text = DMReader["RiskProfile"].ToString();
        T_Investment.Text = DMReader.GetDecimal(DMReader.GetOrdinal("TotalInvestments")).ToString("N2");
        Client_RiskProfile.Text = DMReader["RiskProfile"].ToString();

        //encounter error when i add the datas into arraylist. 
        //System.IndexOutOfRangeException: SectorName

        SectorArray.Add(DMReader.GetOrdinal("SectorName").ToString());
        StockTypeArray.Add(DMReader.GetOrdinal("BlueChipName").ToString());


        foreach( Object objReader in SectorArray){
        //compute the percentage of amount invested in each sector
        //check if the percentage is more than 25%
        //if it is more than 25% lbMsg (an label) shows the name of the sector.

        }
    }

    DMReader.Close();
    DMConnection.Close();
}

当我测试 sql 语句时:

SELECT * FROM ViewSectorInvestments WHERE AccountNumber = @AccountNumber

我得到的结果是:

AccountNumber  SectorName              AmountInvested
1001         Commerce            97230.00000
1001         Construction            389350.00000
1001         Finance             222830.00000
1001         Hotel                     14910.00000
1001         Loans                     105070.00000
1001         Manufacturing           1232210.00000
1001         Mining/Quarrying        32700.00000

我遇到了 System.IndexOutOfRangeException:SectorName。 我的代码有什么问题? 请给我建议。提前致谢。

【问题讨论】:

  • 这里有问题吗?无论如何都很难找到。我建议不要将问题的症结放在代码块中的注释中。
  • 此外,您没有任何代码尝试计算,甚至没有从查询结果中提取计算变量,而且您似乎根本不了解 SqlDataReader 的工作原理。您是否在没有任何经验的情况下编写其他人的代码?
  • 我还没有添加计算代码。目前我什至无法读取这些值。
  • 双 TInvestments = Convert.ToDouble(DMReader["TotalInvestments"].ToString());双 SAmt = Convert.ToDouble(DMReader["SAmountInvested"].ToString());双倍 SPercent = (SAmt / TInvestments) * 100; if (SPercent > 25.0) { lb_Sector2.Text = DMReader["SectorName"].ToString(); }
  • 上面的代码是我想添加到 foreach 循环中的。我不确定出了什么问题。

标签: asp.net c#-4.0 sqldatareader


【解决方案1】:

string DMCommandText = "SELECT Name,RiskProfile,AccountNumber,TotalInvestments FROM ViewClientDetails WHERE AccountNumber = @AccountNumber; SELECT * FROM ViewSectorInvestments WHERE AccountNumber = @AccountNumber ;SELECT * FROM ViewStockTypeInvestments WHERE AccountNumber = @AccountNumber ";

此 CommandText 包含多个查询。只有最后一个 SELECT 语句的结果才会返回给 SqlDataReader。

SectorArray.Add(DMReader.GetOrdinal("SectorName").ToString());

您正在尝试访问 SqlDataReader 中名为“SectorName”的字段的列序号。导致您的异常的问题可能是该列不存在,但很难说,因为您在 CommandText 中使用了 SELECT *。

【讨论】:

  • 这是否意味着我不能有多个选择语句?
猜你喜欢
  • 2016-05-11
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多