我有下面的SQL语句如下:读多行数据使用SqlDataReader的
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:Secto RNAME。 我的代码有什么问题? 请指教我。提前致谢。
这里有问题吗?反正很难找到。我建议不要把你的问题的症结放在你的代码块的评论中。 – pseudocoder 2012-02-02 18:33:53
另外,您没有任何代码尝试计算,甚至没有从查询结果中提取计算变量,而且您似乎也不了解SqlDataReader如何工作。你是否正在使用别人的代码而没有任何经验? – pseudocoder 2012-02-02 18:45:31
我还没有添加计算代码。我甚至不能读现在的价值。 – user1125911 2012-02-02 18:46:59