2012-02-02 3 views
0

나는 다음과 같이 아래 SQL 문을했습니다 : ViewSectorInvestments에서SqlDataReader 개체

SELECT * FROM ViewSectorInvestments WHERE AccountNumber = @AccountNumber 

필드 : I는 각 분야에 대한에 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 이름. 내 코드에 어떤 문제가 있습니까? 제발 조언 해주세요. 미리 감사드립니다.

+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

그래서 여러 가지 선택 진술을 할 수 없다는 뜻입니까? – user1125911