select 部门,sum(工资) from table group by 部门
上面的sql语句应该是对部门进行分组求工资的总和,
假如分组后有3个部门,用C#语言如何获取这三个部门的工资总和?
也就是要获取三个数值.最好把这三个数值存入变量 i1,i2,i3中.
我是新手,希望高手能说详细些.
SqlConnection conn = new SqlConnection(连接字符串);
conn.Open();
SqlCommand cmd = new SqlCommand("select 部门,sum(工资) as 工资和 from table group by 部门", conn);
SqlDataReader dr = cmd.ExexuteReader();
dr.Read();
int i1 = int.Parse(dr["工资和"],ToString());
dr.Read();
int i2 = int.Parse(dr["工资和"],ToString());
dr.Read();
int i3 = int.Parse(dr["工资和"],ToString());
SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=123456;");
conn.Open();
SqlCommand cmd = new SqlCommand("select 部门,sum(工资)as 总工资 from table group by 部门",conn);
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
int i1 = Convert.ToInt32(ds.Tables[0].Rows[0]["总工资"].ToString());
int i2 = Convert.ToInt32(ds.Tables[0].Rows[1]["总工资"].ToString());
int i3 = Convert.ToInt32(ds.Tables[0].Rows[2]["总工资"].ToString());
conn.Close();
非常感谢两位的帮忙.