How to find out if an SqlCe query has rows?

In my simple db, I am using SqlCE, and I cannot figure out how to correctly figure out whether the query returns strings or not. HasRowsdoes 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())
    {
        //
    }
}

thank

+3
source share
2 answers

Try the following:

_DbCommand.CommandText="SELECT COUNT(*) FROM X"
Int32 count = (Int32) _DbCommand.ExecuteScalar();
+2
source
int count = 0;
while (reader.Read())
{
 count++;
}
if(count==0)
{
 // no rows
}
+1
source

Source: https://habr.com/ru/post/1737812/


All Articles