繁体   English   中英

将查询结果保存到变量中

[英]Save Query result in a variable

下面是查询正在使用。 这是根据status列中的数据获取weightagepercentage列的总和。

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个变量中,将值传递和传递失败,并对其进行比较以取得最大效果。 是否可以这样做或我必须更改某些内容?

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")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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