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
if (reader.HasRows)is redundant. When you saywhile (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.