Monday, March 26, 2012

Getting sum data from sql

I'm looking to do a command like this:

string selectCmd ="SELECT SUM(hours) FROM tasks WHERE project=" + project;

string strConnection =ConfigurationManager.ConnectionStrings["TimeAccountingConnectionString"].ConnectionString;

SqlConnection myConnection =newSqlConnection(strConnection);SqlCommand myCommand =newSqlCommand(selectCmd, myConnection);

myConnection.Open();

I would like the function to spit out the sum of the hours as the select command says, but is Open() the right syntax and how would i go about assigning the result to a variable? I'm familiar when it deals with actually returned data, but not a single number.

Int32 count = (Int32) myCommand.ExecuteScalar();
|||

Do a quick F1 search for the ExecuteScalar method of SqlCommand...

|||

great Thanks!

No comments:

Post a Comment