将查询结果保存在变量中

时间:2013-11-08 10:48:52

标签: c# mysql

以下是我正在使用的查询。我根据weightagepercentage列中的数据得到status列的总和。

Status|WeightPer
___________________
P     | 2
F     | 3
F     | 1
P     | 1


SELECT Status, 
       SUM(WeightagePercent) AS Final
FROM ProoferTbl
GROUP BY Status

决赛

F=4
P=3

我需要将结果保存在2个变量中传递和失败并将值与最大值进行比较。是可以这样做还是我必须改变一些东西?

1 个答案:

答案 0 :(得分:0)

SqlConnection con = new SqlConnection("...");
string strSQL = "SELECT SUM(case when Status = 'P' then WeightagePercent else 0 end) as passed, " + 
                "       SUM(case when Status = 'F' then WeightagePercent else 0 end) as failed " + 
                "FROM ProoferTbl2 " + 
                "GROUP BY Status";
SqlCommand cmd = new SqlCommand(strSQL, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();   
int passed = 0, failed = 0; 
while (reader.Read())
{
    passed = (int) reader["passed"];
    failed = (int) reader["failed"];
}
reader.Close();
con.Close();
If(passed > failed)
{ 
    Messagebox.show("Pass")
}