如何在没有dataGridView的情况下将列汇总到表中,并且我使用sql命令方法并且我的数据库在SQL Server 2008中。 我喜欢this script
答案 0 :(得分:0)
您可以遍历DataTable
并对您喜欢的字段求和。
你可以试试这样的东西,
string query = "SELECT Price FROM Bill";
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, DBconn);
DataTable dtSource = new DataTable();
dAdapter.Fill(dtSource);
int colSum =0;
foreach (DataRow dr in dtSource .Rows)
{
colSum += Convert.ToInt32(dr["Price"]);
}
TotalValueLabel.Text = colSum.ToString();
对于乘法,你可以试试这样的东西,
string query = "SELECT Qty,Price FROM Bill";
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, DBconn);
DataTable dtSource = new DataTable();
dAdapter.Fill(dtSource);
dtSource.Columns.Add("3rdColumn");
foreach (DataRow dr in dtSource .Rows)
{
dr["3rdColumn"]= Convert.ToInt32(dr["Qty"]) * Convert.ToInt32(dr["Price"]);
}
答案 1 :(得分:0)
using System.Data;
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("Select Price from PriceList", connection);
sda.Fill(dt);
int sum=Convert.ToInt32(dt.Compute("sum(Price)",""));
Label1.Text=sum.ToString();