将列的值添加到另一列

时间:2015-08-19 10:18:18

标签: c# sql

我在hours中有一个名为sql的列,所以当我通过c#visual studio输入值时,我需要total hours我需要在窗体中显示。

 private void button6_Click(object sender, EventArgs e)
    {
        DateTime date1;
        DateTime date2;
        if (DateTime.TryParse(textBox7.Text, out date1) && DateTime.TryParse(textBox8.Text, out date2))
            textBox6.Text = (date2 - date1).ToString();
        else
            textBox6.Text = "Invalid format";
    }

1 个答案:

答案 0 :(得分:0)

private void button6_Click(object sender, EventArgs e)
{
Sqlconnection con=new SqlConnection(strcon);
con.Open();
SqlCommand cmd=new SqlCommand("SELECT SUM(column_name)as TotalHours FROM   table_name",con);
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
Label1.Text = dr[0].toString();
}
}