I am having confusion in ExecuteScalar & ExecuteReader

If we use ExecuteReader for select with aggregate function, it will not give any error and run successfully so if we can do both queries select with aggregate & select with non aggregate with the help of only one method ExecuteReader so why is there ExecuteScalar………, is there any other benefit of it?

Please give the proper answer

The main difference is the "footprint" of the return value. ExecuteReader always returns an object, for example, a SqlDataReader. Then, to get the actual results, you have to do a Read(), then get the value.

ExecuteScalar immediately returns the actual value.

So, in terms of retrieving Scalar data from a database, ExecuteScalar is more efficient.

Thanx......

The main difference is the "footprint" of the return value. ExecuteReader always returns an object, for example, a SqlDataReader. Then, to get the actual results, you have to do a Read(), then get the value.

ExecuteScalar immediately returns the actual value.

So, in terms of retrieving Scalar data from a database, ExecuteScalar is more efficient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.