Link to home
Start Free TrialLog in
Avatar of run2004
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!
Avatar of adathelad
adathelad
Flag of United Kingdom of Great Britain and Northern Ireland image

>> How can I check if no rows are returned from the database
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.
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
Avatar of nepali
nepali

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of run2004
run2004

ASKER

Thanks for the help Nepali and adathelad. I tried the last solution and this worked for me.