如何在Asp.net C#中使用SQL Server计数功能 [英] How can we use sql server count function in Asp.net c#

查看:219
本文介绍了如何在Asp.net C#中使用SQL Server计数功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在Asp.net c#中使用sql server count函数对表记录进行计数...

How can we use sql server count function in Asp.net c# to count table records...

推荐答案

幸运的是,这非常简单:

Luckily, it''s pretty easy:

using (SqlConnection conn = new SqlConnection(connString))
{
    SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM wherever", conn);
    conn.Open();
    int count  = (int)cmd.ExecuteScalar();
}



显然,您需要修改SQL并提供connString,我的示例中也缺少常规的异常处理.



Obviously, you need to modify the SQL and provide connString, the usual exception handling is also missing from my sample.


这篇关于如何在Asp.net C#中使用SQL Server计数功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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