2016-04-23 7 views
0

の2番目のselectステートメントの値を取得する方法がわかりませんSqlCommandの2番目のselectステートメントから、最初のテーブルから得られた合計ポイントを合計する値を取得しようとしています。 TOTALPOSSIBLE_SUM変数を使用して5番目の要素をプルしようとしましたが、例外がスローされました。SqlCommand C#

これら2つのSUM値を取得する方法についてのアイデアはありますか?あなたはバッチSQL文の結果を読み込むときおかげ

 const int COL_NAME = 1; 
     const int COL_DUE_DATE = 2; 
     const int COL_POINTS_POSSIBLE = 3; 
     const int COL_POINTS_AWARDED = 4; 
     const int TOTALPOSSIBLE_SUM = 5; 
     const int TOTALAWARDED_SUM = 6; 


     // SQL statement to select the columns from the table 
     command.CommandText = 
      "SELECT * FROM " + 
      "[dbo].[Assignments_Table] ORDER BY [PkID] DESC;" + 
      "SELECT SUM(PointsPossible), SUM(PointsAwarded) " + 
      "FROM [dbo].[Assignments_Table]"; 

     assignments_view.Items.Clear(); 

     // SQL reader to read the data from the database into the list view 
     using (SqlDataReader reader = command.ExecuteReader()) 
     { 
      if (reader.HasRows) 
      { 
       while (reader.Read()) 
       { 
        Assignment newAssignment = new Assignment(); 
        ListViewItem assignmentItem = new ListViewItem(); 
        // ID 
        newAssignment.ID = reader.GetInt32(COL_PKID); 
        // Name 
        newAssignment.Name = reader.GetString(COL_NAME); 
        // Due Date 
        newAssignment.DueDate = reader.GetDateTime(COL_DUE_DATE); 
        // Possible Points 
        newAssignment.PointsPossible = reader.GetInt32(COL_POINTS_POSSIBLE); 
        // Awarded Points 
        newAssignment.PointsAwarded = reader.GetInt32(COL_POINTS_AWARDED); 

        newAssignment.TotalPointsPossible = reader.GetInt32(TOTALPOSSIBLE_SUM); 
        newAssignment.TotalPointsAwarded = reader.GetInt32(TOTALAWARDED_SUM); 
       } 
      } 
     } 

答えて

2

は、あなたが結果セット内の次の結果でDataReaderを配置するNextResultメソッドを使用することができます。

while (reader.Read()) 
{ 
    //Write logic to process data for the first result.  
} 


reader.NextResult(); // next resultset 

while (reader.Read()) 
{ 
    //Write logic to process data for the second result. 
} 

NextResult戻り複数の結果セットがある場合、それ以外の場合はfalseですので、これを使用することをお勧めします。

if(reader.NextResult()) // next resultset 
{ 
    while (reader.Read()) 
    { 
      //Write logic to process data for the second result. 
    } 
} 
+0

ハリ、これは完全にあなたにとても感謝します! – Learn12

関連する問題