C# 返回表中的行数返回-1

C# 返回表中的行数返回-1,c#,ado.net,C#,Ado.net,您好,我正在尝试计算表中的行数,但我总是返回-1。以下是我的代码: using (connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand("CountBooks", connection); command.CommandType = CommandType.StoredProcedure;

您好,我正在尝试计算表中的行数,但我总是返回-1。以下是我的代码:

using (connection = new SqlConnection(connectionString))
{
      connection.Open();
      SqlCommand command = new SqlCommand("CountBooks", connection);
      command.CommandType = CommandType.StoredProcedure;
      numberOfBooks = command.ExecuteNonQuery();
}
我的问题是:

SELECT COUNT(*) FROM Books

如何获得正确的结果?

使用ExecuteScalar而不是ExecuteOnQuery。

使用ExecuteScalar而不是ExecuteOnQuery