SELECT MAX(*) TO A VAR

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

Hi,

I'm newbie on C#...
Can anybody tell me with a code example how to make a select max(*) and put
it on a variable.

Thnks a lot!

David C.
 
No excuse, Select MAX(companyID) from costumer where company='1' for
example...
 
That looks better :)

SqlConnection _con = new SqlConnection(Blah...);
SqlCommand _com = new SqlCommand("Select MAX(companyID) from costumer
where company='1'", _con);
_con.Open();
int _maxcompanyid = (int)_com.ExecuteScalar();
_con.Close();

The ExecuteScalar() method returns the value of the first column of the
first row of the result set as an object and therefore you need to cast it
to the appropriate type depending, in this case, on the declared datatype of
the companyID column.

I have demonstrated the case where it might be an int but you need to modify
that part accordingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top