2012-02-02 62 views
0

我有下面的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。 我的代碼有什麼問題? 請指教我。提前致謝。

+0

這裏有問題嗎?反正很難找到。我建議不要把你的問題的癥結放在你的代碼塊的評論中。 – pseudocoder 2012-02-02 18:33:53

+0

另外,您沒有任何代碼嘗試計算,甚至沒有從查詢結果中提取計算變量,而且您似乎也不瞭解SqlDataReader如何工作。你是否正在使用別人的代碼而沒有任何經驗? – pseudocoder 2012-02-02 18:45:31

+0

我還沒有添加計算代碼。我甚至不能讀現在的價值。 – user1125911 2012-02-02 18:46:59

回答

0

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 *。

+0

那麼這意味着我不能有多個選擇語句? – user1125911 2012-02-02 19:02:00