1

I have a stored procedure which returns a certain number of rows.

Case 1: When I use SqlDataAdapter

SqlDataAdapter sdAdapter = new SqlDataAdapter();
ds = new DataSet();
sdAdapter.SelectCommand = myCommand;
sdAdapter.Fill(ds);
int recordCount = ds.Tables[0].Rows.Count;

Case 2: When I use SqlDataReader

SqlDataReader reader = myCommand.ExecuteReader();
if (reader.HasRows)
{
   while (reader.Read())
    {
     recordCount++;
    }
 }

In Case 1, the recordCount is 15 which is correct.

In Case 2, for some reason, reader.HasRows is returning false.

Am I doing anything wrong in terms of syntax? I am confident that myCommand has been built properly since I do get the count in Case 1.

Any help would be really appreciated.

Thank you

3
  • can you perhaps show the sql command that you are using.. Commented Feb 16, 2013 at 1:47
  • 1
    impossible to say from the code you posted... Commented Feb 16, 2013 at 1:47
  • Hi, if I comment the reader.HasRows in Case 2, I am still able to perform reader.Read() and get the value of recordCount. So just wondering is it is possible that hasRows return false but there is still reader pointing to the contents of the query? Commented Feb 16, 2013 at 2:49

2 Answers 2

1

1.A DataReader works in a connected environment, whereas DataSet works in a disconnected environment. 2.A DataSet represents an in-memory cache of data consisting of any number of inter related DataTable objects. A DataTable object represents a tabular block of in-memory data.

more detail go to sqldataadapter or sqldatareader

Sign up to request clarification or add additional context in comments.

Comments

0

if (reader.HasRows) is redundant. When you say while (reader.Read()), it will only loop if there are any rows. Also, this link will explain that HasRows requires a scrollable cursor. Read about the cursors here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.