我需要计算一列的总和并将其显示在total.Text
. 我怎样才能做到这一点?此列有无限的数据,可以随时更改。我正在使用VS2010。我是 C# 的新手。
例子:
_____________________
| last_retail_price |
---------------------
| 500 |
| 200 |
| 5.60 |
---------------------
total.Text = 705.6 \\ The sum of column which I need
我的代码:
private void add_click(object sender, EventArgs e)
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlDataAdapter da = new SqlDataAdapter();
DataTable tl = new DataTable();
da.SelectCommand = new SqlCommand("Select last_ret_prc from pur_temp", con);
con.Open();
da.Fill(tl);
object sum_obj;
sum_obj = tl.Compute("sum(last_ret_prc)");
total.Text = sum_obj.ToString();
con.close();
}