如何计算从c#中的sql表的行数? [英] How to count the number of rows from sql table in c#?

查看:93
本文介绍了如何计算从c#中的sql表的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算c#中sql表的行数?
我需要从我的数据库中提取一些数据...

How to count the number of rows from sql table in c#? I need to extract some data from my database...

推荐答案

你可以这样尝试:

select count(*) from tablename where columname = 'values'

C#代码将是这样: -

C# code will be something like this:-

public int A()
{
   string stmt = "SELECT COUNT(*) FROM dbo.tablename";
   int count = 0;

   using(SqlConnection thisConnection = new SqlConnection("Data Source=DATASOURCE"))
   {
       using(SqlCommand cmdCount = new SqlCommand(stmt, thisConnection))
       {
           thisConnection.Open();
           count = (int)cmdCount.ExecuteScalar();
       }
   }
   return count;
}

这篇关于如何计算从c#中的sql表的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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