ASP.NET WebForm中的案例查询SQL命令

时间:2018-10-09 07:19:51

标签: asp.net sql-server

我已经在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();

1 个答案:

答案 0 :(得分:0)

回答。在ado.net代码中使用内联查询。

    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();
   }