Home > Enterprise >  C# Example of returning the count of items in a column meeting criteria in Access Database
C# Example of returning the count of items in a column meeting criteria in Access Database

Time:06-02

The sql statement "Select Count(Marked) from Results Where Marked = true" is supposed to return the count of items marked. But I am getting 0 where there should be ten. I can see the ten marked items. I tried several forms of the sql statement. I get no errors, just 0. This is an Access database.

        sql = "SELECT COUNT(*) FROM Results WHERE Marked = true";
        cmd = new OleDbCommand(sql, con);
        Int32 num = (Int32)cmd.ExecuteNonQuery();
        con.Close();
        return num != 0;

I cannot find an example using actual C# code so I am not sure the syntax is correct.

CodePudding user response:

Try this

sql = "SELECT COUNT(*) FROM Results WHERE Marked = true";
cmd = new OleDbCommand(sql, con);
//Int32 num = (Int32)cmd.ExecuteNonQuery();
Int32 num = (Int32)cmd.ExecuteScalar();
con.Close();
return num != 0;
  • Related