There are two ways to do this based on the choice of sql execution.
1) If you use ExecuteNonQuery, than use OUTparam in sproc, as shown below.
CREATE PROCEDURE GetEmployee
@employeeID INT,
@Message VarChar(100) OUTPUT
AS
BEGIN
SELECT Emp_Name From Employee Where EmpId = @employeeID
SELECT @Message = 'Your Message!'
END
Now, to get the code @Messagewith in your code OUT, as shown below.
SqlParameter outPutParameter = new SqlParameter();
outPutParameter.ParameterName = "@Message";
outPutParameter.SqlDbType = System.Data.SqlDbType.Varchar;
outPutParameter.Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(outPutParameter);
cmd.ExecuteNonQuery();
string Message = outPutParameter.Value.ToString();
2) If you use a reader than ....
Define a message and use the Select statement instead of print.Like below '
SELECT Emp_Name From Employee Where EmpId = @employeeID
SELECT @Message
To get into your code just use
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
}
if(reader.NextResult())
{
while (reader.Read())
{
}
}
}
source
share