C#ASP.NET怎么使用count函数,然后将计算结果用GridView显示出来

SqlConnection conn = new SqlConnection(strCon);
conn.Open();
string sql = "select count(学号)as人数 from 学生表"
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
checkview.DataSource = ds.Tables[0].DefaultView;
checkview.DataBind();
cmd.Dispose();
conn.Close();
conn.Dispose();
这是我的程序,为什么GridView不显示呢

把sql语句写进SqlDataSource里,下面给VB.net的例子
触发button的点击事件,执行如下代码
Dim sql As String
sql = "select * from tableA where id >''"

If Not textbox1.text.trim = "" Then
sql = sql & " and colB = '" & textbox1.Text.trim & "'"
End If

sql = sql & " order by id desc"
SqlDataSource7.SelectCommand = sql
GridView1.DataSourceID = "SqlDataSource7"
GridView1.DataBind()
温馨提示:内容为网友见解,仅供参考
第1个回答  2017-11-30
checkview.DataSource = ds.Tables[0];
相似回答