4

I am trying to print something if the number of rows returned is more than 0 based on a query:

using (SqlConnection con = new SqlConnection("ConnectionString")){
  con.Open();
  string query = "SELECT COUNT(*) FROM Some_Table WHERE Val > 5";
  SqlCommand cmd = new SqlCommand(query, con);
  cmd.ExecuteNonQuery(); // get the value of the count
  if (count > 0) 
  {
    Console.WriteLine("Returned more than 0 rows");
  }
  else
  {
    Console.WriteLine("Did not return more than 0 rows");
  }
  Console.ReadLine();
}

How can I find the number of rows returned?

2

4 Answers 4

6

Your query always return one row even no rows exists. It will return 0 if no rows exists for your WHERE condition

Use SqlCommand.ExecuteScalar Method ()

using (var con = new SqlConnection("ConnectionString"))
{
  con.Open();
  string query = "SELECT COUNT(*) FROM Some_Table WHERE Val > 5";

  using (var cmd = new SqlCommand(query, con))
  {
      int rowsAmount = (int)cmd.ExecuteScalar(); // get the value of the count
      if (rowsAmount > 0) 
      {
          Console.WriteLine("Returned more than 0 rows");
      }
      else
      {
          Console.WriteLine("Did not return more than 0 rows");
      }

      Console.ReadLine();
}

ScalarValue will return first column of first row of query result. So for your query this is more effective method to retrieve information you need.

Sign up to request clarification or add additional context in comments.

2 Comments

The database helper in WebMatrix is a far easier to read and work with than the overly verbose SqlCommand approach - much better for a newcomer as this guy seems to be.
@JamesHarcourt, I think it is opinion based. Instead for newcomer, I think, much better to get a picture how ADO.NET works.
2

You can do this, Because ExecuteNonQuery - returns the number of rows affected.

int numberOfRecords = cmd.ExecuteNonQuery();
if (numberOfRecords  > 0) 
{
  Console.WriteLine("Returned more than 0 rows");
}
else
{
    Console.WriteLine("Did not return more than 0 rows");
}

4 Comments

Won't this return 1?
this not work for OP's query - SELECT COUNT(*) will always return 1 row
Thanks a lot @Sajeetharan.
However, I was having trouble using this.
1

The ExecuteScalar() method of the SQLClient Command object is specifically provided for returning single values from a query. It is more efficient in terms of code and performance.

using (SqlConnection conn = new SqlConnection("ConnectionString"))
{
conn.Open();
string query = "SELECT COUNT(*) FROM Some_Table WHERE Val > 5";
SqlCommand command = new SqlCommand(query, con);
Int32 count = (Int32) cmd.ExecuteScalar()
}

Comments

0

Use DataSet to get the count

SqlCommand cmd = new SqlCommand(query, con);

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();

da.Fill(ds);

var count = ds.Tables[0].Rows.count;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.