简体   繁体   English

SQL Store表中的行数

[英]SQL Store number of rows in a table

I want to know if it is possible to run a SQL query which returns the number of rows round in a table. 我想知道是否可以运行SQL查询,该查询返回表中的行数。 I have a page which upon clicked, it will run sql queries which compares data between 2 tables, thus I will want the user to be notified if one or more table is empty. 我有一个页面,点击后,它将运行SQL查询,比较2个表之间的数据,因此我希望如果一个或多个表为空,将通知用户。

SqlConnection thisConnection = new SqlConnection("Data Source=DATASOURCE");

    SqlCommand nonqueryCommand = thisConnection.CreateCommand();

    try
    {
        thisConnection.Open();
        //sql statement to check if a table is empty
        //stores the count value in a integer X

        if( X < 1 )
        {
        Response.Write("<script>alert('Database X is empty');</script>");
            return;
        }
     }

Qs: Do I use Select Count(*) from Table to retrieve the number of rows in a table? 问:我是否使用Select Count(*) from Table来检索Select Count(*) from Table中的行数?

How do I store the Count (*) value into an Integer? 如何将Count(*)值存储到Integer中?

Thank you in advance. 先感谢您。


I am using SQL Server. 我正在使用SQL Server。

Try something like this: 尝试这样的事情:

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

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

   return count;
}

Again: this works and gives you an accurate count - but it can be very very slow on large tables. 再说一次:这可以工作并为您提供准确的计数 - 但在大型表格上它可能非常慢。

Alternatives: 备择方案:

  • peek into the system catalog views to get an approximate count (not accurate - but fast) 查看系统目录视图以获得大致计数(不准确 - 但速度快)
  • don't count - but just make sure you have at least one row (using SELECT TOP 1.... and making sure you get something back) 不计-但只是确保你至少有一列(使用SELECT TOP 1.... ,并确保你得到的东西回来)

Update: to simply check whether a table contains any rows at all, you could use this TOP 1 approach which should be really fast - even for large tables: 更新:要简单地检查一个表是否包含任何行 ,您可以使用这个TOP 1方法,它应该非常快 - 即使对于大型表:

public bool TableContainsAnyRows()
{
   // define a TOP 1 query - typically by the Primary Key of the table in question
   // using AdventureWorks sample database here
   string stmt = "SELECT TOP 1 [BusinessEntityID] FROM Person.Person ORDER BY [BusinessEntityID]";

   bool containsAnyRows = false;

   // open a connection and execute this query against the database 
   using(SqlConnection _con = new SqlConnection("server=.;database=AdventureWorks2008R2;integrated Security=SSPI;"))
   using(SqlCommand _cmd = new SqlCommand(stmt, _con))
   {
       _con.Open();

       // getting the result of the query
       // if the table contains *any* rows, the result will *NOT* be NULL
       object result = _cmd.ExecuteScalar();
       _con.Close();

       containsAnyRows = (result != null);
   }

   return containsAnyRows;
}
SELECT COUNT(*) FROM yourtable;

然后从结果中检索第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM