views:

162

answers:

2

Hi, In my simple db I use SqlCe and I cannot figure out how to correctly find out whether the query has rows or not. HasRows does not work. So far I have this:

_DbCommand.CommandText="SELECT * FROM X"
SqlCeDataReader reader=_DbCommand.ExecuteQuery();

if (reader.FieldCount!=0) //I thought it could work (O rows - 0 fields?), but its true even with 0 rows
{
    while (reader.Read())
    {
        //
    }
}

Thanks

A: 
int count = 0;
while (reader.Read())
{
 count++;
}
if(count==0)
{
 // no rows
}
John Boker
Yes it is possible..I hoped for some easier way since reader has to know whether at least 1 record was recieved..at least that was what I thought
Tomas
+1  A: 

Try this:

_DbCommand.CommandText="SELECT COUNT(*) FROM X"
Int32 count = (Int32) _DbCommand.ExecuteScalar();
Eric Dahlvang