Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I get computed values in .NET from a sql statement like this:

SQL
select name, earning from customer
compute min(earning), max(earning)


If I execute this insinde management studio, I get the rows from customer and a secound set with the min and max values.

But with a DataReader in .NET, I can get only a table with rows from customer.

Update:
I think, there are 2 result sets from one query. But I don't know how to access to the second.
Posted
Updated 25-Oct-12 0:48am
v2
Comments
StianSandberg 25-Oct-12 5:55am    
FYI: This feature will be removed in the next version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible. Use ROLLUP instead. For more information, see GROUP BY (Transact-SQL).
Sushil Mate 25-Oct-12 6:00am    
Because they are two sql query not one.
BlackMilan 25-Oct-12 6:41am    
No, I think it's one query (from reading doc), but 2 result sets.
Sushil Mate 25-Oct-12 6:43am    
2 result sets means 2 queries. you need to club it together.

If you had just the computed values from sql.

C#
using (SqlConnection conn = new SqlConnection(@"YourDatabaseInfo"))
      {
        conn.Open();
        SqlDataReader myReader = null;
        SqlCommand cmd = new SqlCommand("select name, earning from customer compute min(earning) as [MinEarned], max(earning) as [MaxEarned]", conn);
        myReader = cmd.ExecuteReader();
        while (myReader.Read())
        {
int MinEarned = Convert.ToInt32(myReader["MinEarned"]);
int ManEarned= Convert.ToInt32(myReader["MaxEarned"]);
        }
mtReader.close();
conn.close();
      }
 
Share this answer
 
v5
Comments
BlackMilan 25-Oct-12 6:38am    
Didd you testet your code? - It doesn't work here.
1. The as keyword can't used for the compute clause.
2. The myReader.Read() delivers only the cols name and earning.
Hi

I think this query will help you..


SQL
select name, earning,(select min(earning) from customer) as MinEarning,

(select max(earning) from customer) as MaxEarning from customer   where earning in
( (select min(earning) from customer) ,

(select max(earning) from customer))



Aswathi Narayan
 
Share this answer
 
v3
Comments
BlackMilan 25-Oct-12 6:46am    
I can't verify this from now, but as described above I think, there are 2 result sets from one query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900