简体   繁体   中英

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

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM