日期:2014-05-18  浏览次数:21003 次

c# 多表查询,并写入到另一个表中
技术比较菜,希望大神们,帮帮忙!!多谢多谢!!!

现在有表:biao1,biao2,biao3

biao1至biao9都有liushuihao这个字段

biao1至biao9包含很多条数据

根据liushuihao 把biao1里面所有jine的和,加biao2里面所有jine的和,加biao3里面所有feiyong的和

算出总计后在复制给string

------解决方案--------------------
C# code

string strSQL = "select sum(t1.jine)+sum(t2.jine)+sum(t3.feiyong) as total from 
biao1 t1,biao2 t2,biao3 t3
where t1.liushuihao =t2.liushuihao and t1.liushuihao = t3.liushuihao and t2.liushuihao =t3.liushuihao";
int ret = 0;

using(SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=sa;Database=xxx"))
{
   con.Open();
   using(SqlCommand cmd = new SqlCommand(strSQL,con))
   {
       ret = (int)cmd.ExecuteScalar();
   }
}
string s = "总金额为:"+ret.ToString();