I have a stored procedure which returns whether a student is locked or not:
RETURN @isLocked
I execute this stored procedure like:
public int IsStudentLocked(string studentName, int lockoutTime)
{
SqlConnection connObj = new SqlConnection();
connObj.ConnectionString = Util.StudentDataInsert();
connObj.Open();
SqlCommand comm = new SqlCommand("uspCheckLockout", connObj);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@Studentname", studentName));
comm.Parameters.Add(new SqlParameter("@LockoutTime", lockoutTime));
comm.ExecuteNonQuery();
connObj.Close();
//How can I return the @isLocked value below?
return ((int)(@isLocked));
}
To use the
RETURNstatement in T-SQL (which can ONLY return integer values), you have to add a parameter to retrieve it:However, this is kinda messy (IMO). Usually what I do in this case is to
SELECTthe value that I want as the last statement in my stored procedure. Then I useExecuteScalaron the command object to retrieve the value instead ofExecuteNonQuery.Proc:
Method: