locked
Nesting DataReader HasRows and multiple SQL ResultSets help? RRS feed

  • Question

  • User598748814 posted

    Question? I have a sql stored procedure that returns 4 result sets. If I nest HasRows, one inside the other moving through the result sets, will any rows returned by the 2nd, 3rd, or 4th result sets return true to the 1st hasRows? The first result set has no rows in certain situations, yet it is not moving to the else clause as it I think it should be. Does anyone know the answer?

    Thanks!

    Monday, August 15, 2011 5:57 PM

Answers

  • User3866881 posted

    Try something like this——

    namespace CSharp
    {
        public class Program
        {
            static void Main(string[] args)
            {
                using (SqlCommand cmd = new SqlCommand("Your Select Statement",new SqlConnection("Your conn str")))
                {
                    cmd.Connection.Open();
                    SqlDataReader sr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                    do
                    {
                        while (sr.Read())
                        {
                            //Do what you like....
                        }
                    } while (sr.NextResult());
                }
            }
        }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 17, 2011 3:38 AM

All replies

  • User1224194097 posted

    I think you should do it this way

    SqlDataReader dataReader = null;
    //first result set
    while (dataReader.Read())
    {
        //some stuff here
    }
    //next resultset
    if (dataReader.NextResult())
        while (dataReader.Read())
        {
            //second result rset stuff here
        }
    //next resultset
    if (dataReader.NextResult())
        while (dataReader.Read())
        {
            //second result rset stuff here
        }

     

    Monday, August 15, 2011 7:26 PM
  • User598748814 posted
    this is normally how I would go through the result sets but I was nesting them to help throw error messages and offer other choices in the event that there were no rows existed. Turns out u can nest them. I was hitting the else clause, I jst forgot to output the error message in my code...stupid mistake on my part.
    Tuesday, August 16, 2011 7:30 AM
  • User3866881 posted

    Try something like this——

    namespace CSharp
    {
        public class Program
        {
            static void Main(string[] args)
            {
                using (SqlCommand cmd = new SqlCommand("Your Select Statement",new SqlConnection("Your conn str")))
                {
                    cmd.Connection.Open();
                    SqlDataReader sr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                    do
                    {
                        while (sr.Read())
                        {
                            //Do what you like....
                        }
                    } while (sr.NextResult());
                }
            }
        }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 17, 2011 3:38 AM