C# 返回 Access 数据库中满足条件的列中的项目数的示例

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

sql 语句“Select Count(Marked) from Results Where Marked = true”应该是 return 标记的项目数。但是我得到 0 应该有十个。我可以看到十个标记的项目。我尝试了几种形式的 sql 语句。我没有收到任何错误,只有 0。这是一个 Access 数据库。

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

我找不到使用实际 C# 代码的示例,所以我不确定语法是否正确。

试试这个

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;