将查询结果保存在变量中 [英] Save Query result in a variable

查看:47
本文介绍了将查询结果保存在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我使用的查询.这里根据 status 列中的数据得到 weightagepercentage 列的总和.

Below is the query am using. Here am getting the sum of weightagepercentage column depending on the data in status column.

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 个变量中通过和失败的值并比较它们的最大值.是否有可能这样做,或者我必须改变什么?

I need to save the result in 2 variables Pass and fail with the values and compare them for greatest. Is it possible to do so or do I have to change something?

推荐答案

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

这篇关于将查询结果保存在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆