Yaron Amar
Yaron Amar

Reputation: 181

ADO.NET number of rows that may be returned by a SQL Server select stored procedure

In C# using ado.net, how to know just the number of rows that may be returned by a SQL Server select stored procedure without returning the result set and without changing the stored procedure?

I don't want to read the data at all, I only want the number of rows because the load can consume a lot of memory.

Upvotes: 1

Views: 1485

Answers (2)

Dave Markle
Dave Markle

Reputation: 97771

I'd originally thought that .ExecuteNonQuery() would do it. But since it doesn't work for SELECT statements, the DataReader will probably be your best (fastest) bet.

int count = 0;
using (var dr = new SqlDataReader(cmd)) {
   while (dr.Read()) count++;
}

Upvotes: 1

Nika G.
Nika G.

Reputation: 2384

if u just don't want to load results, create another sql procedure that just returns select Count(*) from etc...

Upvotes: 1

Related Questions