我已经在sqlserver中编写了一个查询,该查询运行良好。现在,我想在按钮上添加此查询,在asp.net中单击以填充网格。怎么写?
这是我的查询
SqlConnection con =新的SqlConnection(“ ConnectionString”); var command =“ SELECT ActName,CASE WHEN SUM(DebitAmount-CreditAmount)> THEN SUM(DebitAmount-CreditAmount)ELSE 0 END作为DebitAmount,当SUM(DebitAmount- CreditAmount)<0 THEN SUM(CreditAmount- DebitAmount)ELSE 0 END AS CreditAmount 来自TblAWSLocalTrans GROUP BY ActName ORDER BY ActName” SqlCommand cmd =新的SqlCommand(command,con); SqlDataAdapter da =新的SqlDataAdapter(); DataTable dt = new DataTable(); da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); con.Close();
答案 0 :(得分:0)
public void btn_click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection("ConnectionString");
var command= "SELECT ActName,CASE WHEN SUM(DebitAmount - CreditAmount ) > THEN
SUM(DebitAmount - CreditAmount)ELSE 0 END AS DebitAmount,CASE WHEN SUM(DebitAmount -
CreditAmount) < 0 THEN SUM(CreditAmount- DebitAmount )ELSE 0 END AS CreditAmount
FROM TblAWSLocalTrans GROUP BY ActName ORDER BY ActName"
SqlCommand cmd = new SqlCommand(command, con);
SqlDataAdapter da= new SqlDataAdapter();
DataTable dt= new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}