当前位置:编程学习 > C#/ASP.NET >>

C# label中显示数据库查询结果

请问一下,我要吧select count(*) from Sample where srze<1000000查询结果放在label,怎么办,具体代码 --------------------编程问答--------------------

SqlCommand com= new SqlCommand(str,new SqlConnection(connectionstring));
com.Connection.Open();
label.text=com.ExecSclar();
--------------------编程问答--------------------

using(SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=sa;Database=Northwind;"))
{
     con.Open();
     string strSQL = "select count(*) from Sample where srze<1000000";
     using(SqlCommand cmd = new SqlCommand(strSQL,con))
     {
          int count = cmd.ExecuteScalar();
          this.Label1.Text = count.ToString();
     }
}
--------------------编程问答-------------------- 差不多就这样了 --------------------编程问答-------------------- SqlConnection con = new SqlConnection(“连接数据库的字符串”);

                 string sql = "select count(*) from Sample where size<1000000";
                 SqlCommand sqlCmd = new SqlCommand(sql, con);
                 SqlDataReader dr = sqlCmd.ExecuteReader();
                 while (dr.Read()) //读取信息
           {
                    Lable.text= dr["size"].ToString();
                 
                   }
   con.Close();  --------------------编程问答--------------------
引用 2 楼 taomanman 的回复:
C# code


using(SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=sa;Database=Northwind;"))
{
     con.Open();
     string strSQL = "select count(*) from Sample where srze<1000000"……


+1 --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,