run2004
asked on
End of record using C#
Hi,
I have created a datareader object that navigates through the records returned by the database. How can I check if no rows are returned from the database. Also please tell me how the following two differ from each other.
1) While(myReader.read())
{
}
2) if(myReader.read())
{
}
Thanks for the help!
I have created a datareader object that navigates through the records returned by the database. How can I check if no rows are returned from the database. Also please tell me how the following two differ from each other.
1) While(myReader.read())
{
}
2) if(myReader.read())
{
}
Thanks for the help!
To put it another way, .Read() will advance the reader onto the next record in the resultset and returns true if it has retrieved another record, or false if there are no more records.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help Nepali and adathelad. I tried the last solution and this worked for me.
You can set a boolean variable like this if you're using a while loop:
bool hasRecords = false;
While(myReader.read())
{
hasRecords = true;
// Do what you need to do with data
}
>>please tell me how the following two differ from each other.
the first one using the while statement will cyle round each record in the resultset. The second one using the if statement will simply read the 1st record in the resultset if there is one.