Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 931
Thomas Avatar asked Feb 14 '26 18:02

Thomas


2 Answers

Try this:

_DbCommand.CommandText="SELECT COUNT(*) FROM X"
Int32 count = (Int32) _DbCommand.ExecuteScalar();
like image 145
Eric Dahlvang Avatar answered Feb 16 '26 08:02

Eric Dahlvang


int count = 0;
while (reader.Read())
{
 count++;
}
if(count==0)
{
 // no rows
}
like image 34
John Boker Avatar answered Feb 16 '26 06:02

John Boker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!