从数据库中获取计数 [英] getting count from database

查看:61
本文介绍了从数据库中获取计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从sql数据库中获取注册成员总数以显示在aspx页上.
请帮助实现这一目标.
福尔是我尝试过的代码
SqlConnection cn =新的SqlConnection();
cn.ConnectionString = ConfigurationManager.ConnectionStrings ["abcConnectionString"].ConnectionString;
字符串sql =从reg中选择count(email)";
SqlCommand cmd =新的SqlCommand(sql,cn);


试试
{

cn.Open();

int memberCount = cmd.ExecuteNonQuery();
lbl_memberCount.Text = memberCount.ToString();
会话["memberCount"] = memberCount;
}
catch(ex ex例外)
{
Response.Write(ex.Message);

}

I want to get total no of registered members from sql database to show on aspx page.
plz help to achieve this..
Foll is the code i tried
SqlConnection cn = new SqlConnection();
cn.ConnectionString = ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString;
String sql = "select count(email) from reg";
SqlCommand cmd = new SqlCommand(sql, cn);


try
{

cn.Open();

int memberCount = cmd.ExecuteNonQuery();
lbl_memberCount.Text = memberCount.ToString();
Session["memberCount"] = memberCount;
}
catch (Exception ex)
{
Response.Write(ex.Message);

}

推荐答案

尝试ExecuteScalar()返回一个值.
Try ExecuteScalar() to return a value.


对您的代码进行此更改...

Make this change to your code...

try
{
cn.Open();
int memberCount = int32.parse(cmd.ExecuteScalar());
lbl_memberCount.Text = memberCount.ToString();
Session["memberCount"] = memberCount;
}
catch (Exception ex)
{
Response.Write(ex.Message);
}


这篇关于从数据库中获取计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆