将列的总和反映到WinForm上的标签

时间:2018-06-29 00:04:24

标签: c# sql-server winforms

我想将列的总和反映到WinForm上的标签上。我不知道这段代码有什么问题:

1.345678

出现的错误异常是:1.345678

1 个答案:

答案 0 :(得分:1)

如果要按名称引用该列,则必须命名SUM的结果。在这种情况下,您也不需要循环,因为您知道只有一行。

private void btnT_Click(object sender, EventArgs e)
{
 SqlConnection cn = new SqlConnection("data source = TURKY-PC ; initial 
 catalog = coffeeshopDB ; integrated security = true ; ");            
 SqlCommand cmd;                      
 SqlDataReader dr;        
 cmd = new SqlCommand("select SUM (cost) as TotalCost from billTB", cn);             
 cn.Open();       
 dr = cmd.ExecuteReader();    
 dr.Read();            
 btnT.Text = dr["TotalCost"].ToString();     
 dr.Close();      
 cn.Close();       
}
相关问题