简体   繁体   中英

Count how many times 'male' and 'female' appears in a field in access database (asp.net, C#)

I would like my asp.net application to count how times 'male' and 'female' appear in a certain field upon button click. This is what I have so far and would like the values to return in a label. How can I return those values in separate labels? One for Male and one for Female.

protected void btnRetrieve_Click(object sender, EventArgs e)
{
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=accessdatabase.mdb";
string cmdstr = "SELECT Gender FROM StudentList WHERE (GENDER = 'Male') OR (GENDER = 'Female')";

OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
MaleLabel = ????

}

Change your query to

select gender, count(gender)
from StudentList
group by gender

At that point you can retrieve the gender and the count directly from the results.

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