Thomas
Thomas

Reputation: 2615

How to find out whether SqlCe query Has rows?

In my simple db I use SqlCE and I cannot figure out how to correctly find out whether the query returns 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

Upvotes: 3

Views: 2469

Answers (2)

Eric Dahlvang
Eric Dahlvang

Reputation: 8292

Try this:

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

Upvotes: 2

John Boker
John Boker

Reputation: 83719

int count = 0;
while (reader.Read())
{
 count++;
}
if(count==0)
{
 // no rows
}

Upvotes: 1

Related Questions