2012-02-02 5 views
0

I次のように以下のSQL文をしました:ViewSectorInvestmentsでSqlDataReaderの

SELECT * FROM ViewSectorInvestments WHERE AccountNumber = @AccountNumber 

フィールド:私は、各セクタに対してでAmountInvestedを計算しようとしている

AccountNumber 
SectorName 
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

+0

また、計算を試みるコードがないか、クエリ結果から計算変数を抽出するコードがなく、SqlDataReaderの動作方法がわからないようです。他の誰かのコードを経験していませんか? – pseudocoder

+0

私は計算のためのコードを追加しません。私はその時点で値を読むことさえできません。 – user1125911

答えて

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

それは私が複数のselect文を持つことができないことを意味しますか? – user1125911

関連する問題