How to handle multiple ResultSets, each with multiple Rows? IDataReader.NextResult() ending Read()

后端 未结 1 1623
遥遥无期
遥遥无期 2020-12-20 12:35

How to handle multiple ResultSets, each with multiple Rows? The call to NextResult() breaks the while loop.

Some of my SPs return multiple ResultSets. I

1条回答
  •  醉梦人生
    2020-12-20 13:12

    You need to create two nested loops.

    • The outer loop should iterate over result sets and should have NextResult at the end
    • The inner loop should iterate over rows in a result set and should have Read at the beginning.

    Something like:

    using (IDataReader reader = ...) { 
      // Process all result sets
      do {
        // Process all elements in the current result set
        while (reader.Read()) { 
          ArrayList row = new ArrayList(); 
          for (int j = 0; j < reader.FieldCount; ++j) { 
            object rowValue = reader.GetValue(j); 
            row.Add(rowValue); 
          } 
          // TODO: Do something with 'row'
        }
      } while (reader.NextResult())
    } 
    

    0 讨论(0)
提交回复
热议问题